-
-
Save jeradg/3ae725bf74a0b6eefefe to your computer and use it in GitHub Desktop.
Computed Properties with @each (working - Ember 2.0.2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
let lastId = 0; | |
export default Ember.Controller.extend({ | |
rootItems: Ember.computed.filter('owner.items', function(item) { | |
return item.get('parent.content') === null; | |
}).property('[email protected]'), | |
owner: Ember.computed(function() { | |
let owner = this.store.createRecord('owner'); | |
this.store.createRecord('item', { | |
id: ++lastId, | |
owner: owner, | |
parent: null | |
}); | |
this.store.createRecord('item', { | |
id: ++lastId, | |
owner: owner, | |
parent: null | |
}); | |
return owner; | |
}), | |
actions: { | |
addRootItem() { | |
return this.store.createRecord('item', { | |
id: `${++lastId}`, | |
owner: this.get('owner'), | |
parent: null | |
}); | |
}, | |
changeParent(item, parentId) { | |
let itemId = item.get('id'); | |
let parent = this.get('owner.items').findBy('id', parentId); | |
if (!parentId) { | |
console.log(`Set parent of item ${itemId} to null`); | |
return item.set('parent', null); | |
} | |
if (_parentIsIllegal(item, parent)) { | |
console.log("Can't set item's parent to itself or one of its descendants"); | |
return; | |
} | |
if (parent) { | |
console.log(`Set parent of item ${itemId} to ${parentId}`); | |
return item.set('parent', parent); | |
} | |
} | |
} | |
}); | |
function _parentIsIllegal(item, newParent) { | |
let itemInAncestors = false; | |
let ancestor = newParent; | |
while (ancestor && !itemInAncestors) { | |
itemInAncestors = ancestor === item; | |
ancestor = ancestor.get('parent.content') || ancestor.get('parent'); | |
} | |
return itemInAncestors; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
import Ember from 'ember'; | |
export default DS.Model.extend({ | |
owner: DS.belongsTo('owner'), | |
parent: DS.belongsTo('item', {inverse: null}), | |
children: Ember.computed.filter('owner.items', function(item) { | |
return item.get('parent.content') === this; | |
}).property('[email protected]') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.Model.extend({ | |
items: DS.hasMany('item') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
actions: { | |
changeParent(newParentId) { | |
let item = this.get('item'); | |
return this.attrs.changeParent(item, newParentId); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.4.17", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.0.2", | |
"ember-data": "2.2.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment