Skip to content

Instantly share code, notes, and snippets.

@kamal
Last active December 20, 2015 06:29
Show Gist options
  • Save kamal/6086455 to your computer and use it in GitHub Desktop.
Save kamal/6086455 to your computer and use it in GitHub Desktop.
test("modifying a hasMany record should make parent dirty", function() {
expect(2);
var Author = Ember.Model.extend({
id: Ember.attr(),
name: Ember.attr()
}),
Post = Ember.Model.extend({
id: Ember.attr(),
authors: Ember.hasMany(Author, {key: 'author_ids'})
});
Post.adapter = Ember.FixtureAdapter.create();
Author.adapter = Ember.FixtureAdapter.create();
var author = Author.create();
var post = Post.create();
author.on('didLoad', function() {
post.get('authors');
ok(!post.get('isDirty'));
author.set('name', 'billy');
ok(post.get('isDirty'));
});
Ember.run(post, post.load, 1, {id: 1, author_ids: [100] });
Ember.run(author, author.load, 100, {id: 100, name: 'bob' });
});
Ember.ManyArray = Ember.RecordArray.extend({
_records: null,
objectAtContent: function(idx) {
var content = get(this, 'content');
if (!content.length) { return; }
return this.materializeRecord(idx);
},
// needed for find
nextObject: function(idx, previousObject, context) {
return this.objectAtContent(idx);
},
save: function() {
// TODO: loop over dirty records only
return Ember.RSVP.all(this.map(function(record) {
return record.save();
}));
},
replaceContent: function(index, removed, added) {
added = Ember.EnumerableUtils.map(added, function(record) {
return record._reference;
}, this);
this._super(index, removed, added);
},
isDirty: Ember.computed(function() {
return this.find(function(record) {
return record.get('isDirty');
});
}).property().volatile()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment