Ember Data Save method, Create vs update

爱⌒轻易说出口 提交于 2019-12-22 11:57:11

问题


I can't figure out how Ember determines if it should update or create a record. I would assume its based on the ID or on the Store entry, but it seems to be something else. The code example clarifies:

// this returns the user without making an api call
currentUser.get('store').find('user_detail', '49')

// this returns 49
currentUser.get('id')

// this returns true
currentUser.get('store').hasRecordForId('user_detail', 49)

// this issues a create to api/userDetails instead
// of updating /api/userDetails/49
currentUser.save()

// maybe this is a lead, not the 48 at the end
currentUser.toString()
// <EmberApp.UserDetail:ember461:48>

// it looks as though currentState is involved here
// http://emberjs.com/api/data/classes/DS.RootState.html
currentUser.currentState

// returns root.loaded.created.uncommitted
currentUser.get('currentState.stateName');

// also isNew is wrong and returns true
currentUser.get('isNew');

Let me explain why I have this issue. My app has a current user. If you logout I update the current user. So I set Ember.currentUser.setProperties(newUserData). I update the currentUser object so that ember automatically triggers updates throughout my app. If I would replace the currentUser Ember.currentUser = newUser; Nothing would update. If I cant solve the above problem an alternative solution for the swapping of the user object would also work.

This is how I handle the global user state

container.register('user:current', Ember.currentUser);
// and handle updates via Ember.currentUser.setProperties()
application.inject('controller', 'user', 'user:current');
application.inject('route', 'user', 'user:current');

A proper solution would replace Ember.currentUser, however doing that doesnt trigger updates.


回答1:


A new model will have the isNew and isDirty properties set to true, an existing record that needs to be updated will only have isDirty set to true.




回答2:


I'd recommend pushing your user one level deeper and not storing it on the Ember namespace, that way you can set it from anywhere else, yet still inject it during injection

var users = Em.Object.create({
  current: currentUser
});

container.register('users:current', users, {instantiate: false});
// and handle updates via Ember.currentUser.setProperties()
application.inject('controller', 'users', 'users:current');
application.inject('route', 'users', 'users:current');

Then from any controller you can access/watch it on users.current, yet you can also set it using this.users.set('current', newUser) which would effect anyone watching that property on any controller or route.

Example: http://emberjs.jsbin.com/OxIDiVU/1145/edit

Additionally a lot of things you are doing are async calls and should use the promise pattern for viewing properties etc.



来源:https://stackoverflow.com/questions/26258352/ember-data-save-method-create-vs-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!