Adding one model to multiple collections - backbone.js

My application has two collection instances, allItems and someItems.
I have populated allItems, and then have added the first model of allItems to someItems, like this:
someItems.add(allItems.get(0).toJSON());
The problem is this creates a duplicate. If I change an attribute of that model in someItems, allItems will not be affected.
How can I effectively link the two?

You should be able to do
someItems.add(allItems.get(0));
to add the model and keep them in sync.

allItems.get(0).toJSON() will create a JSON object which is different from allItems.get(0).
That's why you can't maintain only one object.
Delete .toJSON() and have a try.

Related

How to pass all field values from one view to another by a single object?

I have two views 1.UserInfo 2.EditUserInfo
In view UserInfo there are lots of filed on the view
I want to pass that values to EditUserInfo view using a single object
Is there any in build option in Ext JS to pass all field values from one view to another using single view-object
If your fields are in a formpanel (they better be), the easiest would be to use formpanel.getValues() and formpanel.getForm().setValues().

Update model of a grid store on the fly

A grid is configured with a store that use a model that I can call modelA
I need to change on the fly the model of the store related to the grid.
I followed following approach without results:
grid.getStore().setModel(modelB);
grid.reconfigure(
grid.getStore(),
columns
);
grid.getStore().load();
It continues to use the model defined at the beginning. In fact if I debug the record that is passed to the renderer function as follows:
function(value, metadata, record)...
record continues to reference modelA instead of modelB.
How can I change dynamically the model of the store?
I think that although you set new model you haven't re-read the records, at least you do not show any store.load() call. Models are used by store/proxy/reader to create instances when the store is loaded.
Now, I wouldn't use this approach anyway. Store is not a very expensive in terms of performance or memory so if I'd need to reconfigure the grid I'd use another store with that another model.
To achieve this task, you can change fields of model dynamically instead of changing the model itself. Sample fiddle
store.model.setFields(fieldsArray);
store.load(); // or store.load(newData);
grid.reconfigure(store,columnInfo);
Hope this helps.

Fetch models in backbone collection creation

I have a model and collection, when I create my collection I would like to fetch three models instead of all collections.
If I use collection.fetch() - if will fetch all objects, right now - I am fetching each model and then creating the collection, like this -
var model = new App.Model({ Id: 1 });
model.fetch().success(function() {
var collection = new Collection(model);
});
(This is a simplistic version for one model, if I want to fetch multiple, I use $.when)
There is an official/better way for doing this? or I am at the good path?
I think you can get the models that you want from the original collection and put them in a temp collection. So you can fetch the temp collection once instead fetch each model one by one.
After a little bit of thinking, I saw that my collections have many to one relation to my model.
So I used backbone-relational to fetch all of this collections.

How can I copy a model from one collection to another in Backbone.js

I am trying to copy a model from a backbone collection to another but problem is that only reference is being copies, that is if I change value of model in one collection the value is automatically changed for other collection. The problem is how do I make an exact copy of model object.
Thanks
I have tried all the methods of cloning but the result was not good because cid of the clonned model was becoming same which was causing problem. So I have applied this method
var widget = this.widgetsCollection.get(widgetId)
var newWidget=new Widget(widget.attributes);
This gives a copy with different cid.
Try creating a deep copy, which will create a new object instance with the same values.
An example can be found in this SO thread: What is the most efficient way to deep clone an object in JavaScript?
There also exist a method clone in Backbone Model that creates a new copy with same attributes
this.widgetsActiveCollection.add(widget.clone());
This is how I create a deep copy of the model
var newModel = new createModel(JSON.parse(JSON.stringify(oldModel)));
newCollection.add(newModel );

Backbone.js: How to get the index of a model in a Backbone Collection?

Is there a way to find the index of a model within a collection?
Let's say in a view we have a model we're working on, could that model spit out it's index within the collection it's currently inside of? I'd like to do this because I want to access the model above or below the current target.
In other words is there something like:
index = this.model.index
modelAbove = this.collection.at( index-1 )
My data is a nested set so I can just do a search on the "lft" or "rgt" columns, but I didn't want to reinvent the wheel if Backbone already has this info available.
yes, backbone provides access to many underscore.js methods on models and collections, including an indexOf method on collections. it also provides an at method like you've shown.
var index = this.collection.indexOf(this.model);
var modelAbove = this.collection.at(index-1);

Resources