Skip to content

Instantly share code, notes, and snippets.

@xdissent
Created June 2, 2012 23:02
Show Gist options
  • Save xdissent/2860403 to your computer and use it in GitHub Desktop.
Save xdissent/2860403 to your computer and use it in GitHub Desktop.
Ember-data hasMany computed properties test
test("filtered property on hasMany assoctiation", function() {
var File = DS.Model.extend({
primaryKey: 'name',
name: DS.attr('string'),
status: DS.attr('string')
});
var Project = DS.Model.extend({
primaryKey: 'name',
name: DS.attr('string'),
files: DS.hasMany(File, {embedded: true}),
added: function() {
return this.get('files').filter(function(f) { return (f.get('status') == 'added') });
}.property('files'),
changed: function() {
return this.get('files').filter(function(f) { return (f.get('status') == 'changed') });
}.property('files'),
deleted: function() {
return this.get('files').filter(function(f) { return (f.get('status') == 'deleted') });
}.property('files'),
hasDeleted: function() {
return !!this.getPath('deleted.length');
}.property('deleted.length')
});
var store = DS.Store.create({revision: 4});
store.load(Project, 'chimp', { name: 'chimp', files: [
{ name: '.chimprc', status: 'added' },
{ name: '.rvmrc', status: 'added' },
{ name: 'config.ru', status: 'changed' },
{ name: 'index.php', status: 'deleted' }
] });
var project = store.find(Project, 'chimp');
equal(project.get('name'), 'chimp', 'found the project with the right name');
equal(project.getPath('files.length'), 4, 'has the correct number of files');
equal(project.getPath('added.length'), 2, 'has the correct number of added files');
equal(project.getPath('changed.length'), 1, 'has the correct number of changed files');
equal(project.getPath('deleted.length'), 1, 'has the correct number of deleted files');
equal(project.get('hasDeleted'), true, 'hasDeleted helper is accurate');
project.get('deleted')[0].set('status', 'changed');
equal(project.getPath('deleted.length'), 0, 'filtered hasMany properties are updated if children are updated');
equal(project.get('hasDeleted'), false, 'hasDeleted helper is accurate after update');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment