I want to know that will backbone model cid will unique among multiple collection.
Example: I have pageCollection and each pageCollection will contains another collection called controlCollection.
If I am creating control model, its cid will be unique among all controlCollection inside all pageCollection?
hierarchy:
pageCollection
->pageModel
->controlCollection
->controlModel
Considering same model can be present inside multiple collections by design in backbone, it'll be unique. No other model will have same cid.
Related
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.
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.
I'm having issues. I want to use the nice ExtJS associations, but they're not working properly.
Issues:
no association showing in the model
no data showing up after load
What are the quirks to watch out for?
I recently went through a very painful learning curve with the ExtJS associations, and came across some useful articles, as well as my own gotchas. Here is the summary for those who run into the same pains.
Rules for HasMany Associations in ExtJS
Always put your Proxies in your Models, not your Stores, unless you
have a very good reason not to [1]
Always require your child models if
using them in hasMany relationships. [2]
Always use foreignKey if you want to load the children at will
Always use associationKey if you return the children in the same response as the parent
You can use both foreignKey and associationKey if you like
Always name your hasMany relationships
Always use fully qualified model names in your hasMany relationship
Consider giving the reader root a meaningful name (other than "data")
The child model does not need a belongsTo relationship for the hasMany to work
[1] The store will inherit its model's proxy, and you can always override it
[2] To make it easy, and avoid potential circular references, you can require them in app.js
http://extjs-tutorials.blogspot.com/2012/05/extjs-hasmany-relationships-rules.html
Rules for HasOne and BelongsTo Associations in ExtJS
Put the proxy in the model, unless you have a very good reason not to
Always use fully qualified model name
Always set the getterName
Always set the setterName
Always set the associationKey, if the foreign object is returned in the same response as this object
Always set the foreignKey, if you want to load the foreign object at will
Consider changing the instanceName to something shorter
The getter behaves differently depending on whether the foreign object is loaded
or not. If it's loaded, the foreign object is returned. Otherwise,
you need to pass in a callback to get it.
You should set the name property if you plan to override this association.
You do not need a belongsTo relationship for a hasMany to work
Set the primaryKey property if the id field of the parent model is not "id"
Sometimes you need to use uses or requires for the belongsTo association. Watch
out for circular references though.
Calling setter() function does
not seem to set the instance. Set object.belongsToInstance = obj if
calling the setter().
http://extjs-tutorials.blogspot.com/2012/05/extjs-belongsto-association-rules.html
Misc
If you're applying your data to a grid, make sure you call reconfigure() on the grid using the new store
Your "foreignKey" property will be applied as a local filter to the ExtJS store; if you see the data loading over the network, but
not showing in your grid, make sure your model has the foreignKey
value defined as a field, or the local filter will exclude the data
quiety. To test if this is the case, hook into the store's "load"
event and call store.clearFilters(), and see if your data shows up
I would need an idea or two how I would do this in cakephp (using latest version)
I am building a web based game where you will be able to collect Items
Without a framework I would have an abstract base item class that every item would extend to
And when displaying for example a inventory i would factory all items the user currently have and then return a object for each item.
classes...
BaseItem
WeaponItem
HealingItem
etc..
How would I do this in cakephp? Would I go for a model for each item class ... and how would i factor to get the object? ...
Assuming you're using a database as the data store, presumably you will use a single table for all items the player can collect? If so, you probably want a single Model class.
It's possible to have an inheritance hierarchy for models in CakePHP if you want. But you can often achieve sharing of Model logic using a Behaviour.
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);