blittle / backbone-nested-models Goto Github PK
View Code? Open in Web Editor NEWBackbone Nested Models
Backbone Nested Models
var someModel = Backbone.Model.extend({
__defaults: {
____attr1: "comment",
____attr2: false
__},
__relations: {
____someRelation: Backbone.Collection
__},
__initialize: function() {
____this.get("attr1") // work well
____this.get("someRelation") // returm undefined
__}
});
It's currently not possible to silently reset a model with nested data, as any nested things created will be created noisily, as the options object with silent=true are not passed along.
I ended up doing this:
model.set(rModel.toJSON ? rModel.toJSON() : rModel);
// Remove the model from the incoming list because all remaining models
// will be added to the relation
modelsToAdd.splice(i,1);
} else {
modelsToRemove.push(model);
}
});
_.each(modelsToRemove, function(model) {
- relation.remove(model);
+ relation.remove(model, options);
});
- relation.add(modelsToAdd);
+ relation.add(modelsToAdd, options);
} else {
// The incoming val that is being set is not an array or collection, then it represents
// a single model. Go through each of the models in the existing relation and remove
// all models that aren't the same as this one (by id). If it is the same, call set on that
// model.
relation.each(function(model) {
if(val[id] === model[id]) {
- model.set(val);
+ model.set(val, options);
} else {
- relation.remove(model);
+ relation.remove(model, options);
}
});
}
return relation;
}
if(relation && relation instanceof Model) {
- relation.set(val);
+ relation.set(val, options);
return relation;
}
options._parent = this;
val = new this.relations[attr](val, options);
val.parent = this;
}
return val;
};
Does that seem viable?
Currently parent
is added to the newly created collection/model as a property, but this means that there is no way to know what the parent is within the initialize
method.
Maybe just adding parent
to the options is a reasonable solution?
Similar to dhruvaray/backbone-associations#32, I'd like to know what's the main difference between backbone-nested and backbone-relational/backbone-associations?
I'm struggling to get my head around how backbone-nested is dealing with firing events on nested Backbone structures.
As far as I can tell, no reset
events are fired for nested collections, when the parent collection is reset.
Am I right in thinking that this is currently a shortcoming in the library? Would be great to have this functionality, currently I'm having to do strange things like listening to events on the top level collection, and I don't think I'm going to be able to get much further with that.
test
I have this objects definition:
var Item = Backbone.Model.extend({
relations: {
"Outlooks": OutlookCollection
}
});
var OutlookCollection = Backbone.Collection.extend({
model: Outlook
});
var Outlook = Backbone.Model.extend(
relations: {
"Scenarios": Backbone.Collection
}
);
when I fetch an Item the attribute Outlook is an OutlookCollection but in every Outlook the attribute Scenarios is an array not a Backbone.Collection.
Should work it or I'm missing something?
This causes their url property to be overridden with an erroneous url.
I had to temporarily fix this by removing the url property from the options object before doing setRelation
.
Scotts-iMac:frimfram sderickson$ bower install --save backbone-nested-models
bower cached git://github.com/dtmtec/backbone-nested-attributes.git#0.5.0
bower validate 0.5.0 against git://github.com/dtmtec/backbone-nested-attributes.git#*
I have an options
model in which one of the options is a center
. I've set this up to be a center
model in the relations.
When I change the center
using set
, nothing is fired from the events. If I change another attribute in the options
model, events are fired normally.
Am I missing something here?
see the below example:
var Author = Backbone.Model.extend({
defaults: {
name: 'test',
age: 20
}
});
var Book = Backbone.Model.extend({
relations: {
author: Author
},
defaults: {
author: new Author()
}
});
var book = new Book({name: 'go!'});
book.toJSON().author
is a Backbone.Model like instance (because instanceof Backbone.Model
returns false), instead of the JSON of the defaults of Author.
it seems that book.attributes.author = new Author(new Author)
backbone-nested
on NPM is not this library, despite the package.json saying so.
I managed to pull down this module using Napa, but it appears the UMD wrapper is broken.
I am using webpack (common.js). :)
Does you library have any relation (no pun intended) with Bakbone-Nested (http://afeld.github.com/backbone-nested/) ? I just came across your project when searching for the other (older) one and was confused, not recognizing what I was looking at :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.