Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blank modelName when collection uses generic BaseModel #151

Open
spikebrehm opened this issue Oct 17, 2013 · 2 comments · May be fixed by #253
Open

Fix blank modelName when collection uses generic BaseModel #151

spikebrehm opened this issue Oct 17, 2013 · 2 comments · May be fixed by #253
Labels

Comments

@spikebrehm
Copy link
Member

In ModelStore#set, we determine the cache key based on the type of model. This is done using modelUtils.modelName(model), which in turn looks at the id or name property of model.constructor.

Now, in the case where a collection does not specify a custom model class, it will just use BaseModel. A problem arises when ModelStore#set is called on a model of this collection, because its modelName will be blank (it's an empty string in Chrome, and undefined in IE because IE doesn't support constructor.name). So IE throws an error, and Chrome just uses a bad cache key, i.e. ":14" instead of "my_model:14", in the case where model.id = 14.

Sample collection:

var BaseCollection = require('rendr/shared/base/collection');
module.exports = BaseCollection.extend({});

Solution: modelUtils.modelName should fall back to looking at model.collection.constructor.id if model.constructor.id is blank.

@lo1tuma
Copy link
Member

lo1tuma commented Jan 5, 2014

@spikebrehm the BaseCollection in your example doesn’t have an id too. So your proposed solution will probably fail.

Why we don’t simply provide an id for BaseCollection and BaseModel? Maybe a uuid?

@jskulski
Copy link
Contributor

jskulski commented Jan 6, 2014

I like the idea of having a default id. A lot of things break when you have models without IDs.

I ran into that playing around with view create viewModels that worked fine until a page switch (when things are set and fetched from the stores). Took me a while to track it down and the solution was to do something weird like

defaults: { 
  id: 'lolz'
}

If it's so important an error should be thrown, or a default should provided. I think in this case a unique default is the right solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants