ember.js - hasMany/belongsTo: how do i change the ownership of an item (move from one parent to another)? -


i've got model both hasmany , belongsto related

todos.todo = ds.model.extend({   title: ds.attr('string'),   iscompleted: ds.attr('boolean'),   parent: ds.belongsto('todo', {inverse: 'children'}),   children: ds.hasmany('todo', {inverse: 'parent'}) }); 

i going let user drag-n-drop todos on each other let him rearrange hierarchy. that's difficult task person not familiar ember, decided start simpler:

each todo contains dropdown list of possible parents. user can select parent list , todo gets updated parent.

there's "no parent" item in dropdown list. when selected todo, todo updated contain no parent.

the way perform modification of parent of todo pretty straightforward:

  1. grab collection of models todos arraycontroller (controllers.todos.model).
  2. filter collection contains record id equal requested id (from user's choice on dropdown list).
  3. grab first , record filtered collection.
  4. set current record's parent field` grabbed record object.
  5. save currrent record.

i have created couple of simple properties on todo controller see whether each todo has parent , children:

  haschildren: (function() {     return this.get('model.children').get('length') > 0;   }).property('model.parent', 'model.children'),    hasparent: (function() {     return this.get('model.parent') !== null;   }).property('model.parent', 'model.children'), 

from hasparent property can tell parent modified when change todo's parent using dropdown list, yay! modification persists through page refreshes (i'm using local storage adapter), assume perform updating todo's parent correctly.

the problem haschildren property on todo b not udpated when update 'parent' property on todo contain todo b. prevents me automatically refreshing hierarchy of todos on page.

i used believe ember data should automatically update children property on parent when update parent property on child. that's inverse thingie for, right? if ember data not supposed automatically maintain integrity of relationship, why want know opposite properties of relationship?

so either i'm wrong , should manually update children property of old , new parents when update parent property on child todo (risking damage integrity of relationships), or i'm changing parent in wrong way.

the question is: correct way of changing ownership of item in hasmany/belongsto relationship?

fiddle: http://jsbin.com/udopaja/220/edit

upd1 ppcano:

i've tried approach , made no difference. :( have applied suggestions, , model hasparent property updated dynamically. haschildren property doesn't.

but tried setting haschildren property observe todos.@each.parent , started update, yay!

what still don't understand: in order change relation of record have lot of work. how applied suggestion:

var model = this.get('model');  // removing current todo list of children of former parent model   .get('parent')   .get('children')   .removeobject(model);  // adding current todo list of children of new parent   .get('todos')   .filter( function(candidatetodo) {     return candidatetodo.get('id') === newparentid;   })[0]   .pushobject(model);  model.save();     

this hell lot of work , more prone breaking integrity of relationships. why can't instead?

var model = this.get('model'); var newparent =   .get('todos')   .filter( function(candidatetodo) {     return candidatetodo.get('id') === newparentid;   })[0];  model.set('parent', newparent); model.save(); 

i've tried , won't work. :( parent property gets updated, children property won't.

is there way update relationship belognsto end rather doing job update relationship hasmany end.

i've found this answer means manual updating on both ends , don' either. asker there surprised ember requires doing manually.

there's weirder thing. in order update current model's parent property, have modify relationship on opposite end (call method on children property on parent). if on parent, why required .save() on current model , not parent model in order changes persist? , why haschildren property have observe todos.@each.parent , observing model.children not work?

there's unsolved problem: when new todo added or existing todo deleted, "parent:" dropdown lists of todos reset "no parent" state! if refresh page, correct parent selection in dropdowns restored. whaaat?

please me resolve these issues. fiddle: http://jsbin.com/udopaja/231/edit

if setup inverse property, need push/remove objects relationship , ember-data take care update dependent relationship.

parent.get('children').then(function(children){   children.removeobject(child); })  parent.get('children').then(function(children){   children.pushobject(child); }) 

because of current status of ember-data (using v1.0.0-beta.8), define hasparent , haschildren as:

  haschildren: ember.computed.notempty('children.[]'),   hasparent: ember.computed.notempty('parent.content') 

look @ example: http://emberjs.jsbin.com/pubij/2/edit


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -