灰烬数据保存方法,创建VS更新 [英] Ember Data Save method, Create vs update

查看:136
本文介绍了灰烬数据保存方法,创建VS更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何烬确定是否应更新或创建一个记录。我会承担其基于ID或在商店条目,但它似乎是别的东西。在code例如澄清:

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');

让我解释为什么我有这个问题。我的应用程序有一个当前用户。如果你退出我更新当前用户。所以我设置Ember.currentUser.setProperties(newUserData)。我更新的currentUser对象,以便在整个余烬我的应用程序会自动触发更新。如果我将取代的currentUser Ember.currentUser = NEWUSER;没有什么会更新。如果我不能解决上述问题的用户对象的交换的替代解决方案也将工作。

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');

一个妥善的解决方案将取代Ember.currentUser,但是这样做,这并不触发更新。

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

推荐答案

我建议你推你的用户更深层次的原因不是将它们储存在灰烬的命名空间,这样你可以从其他地方设置,但仍然注入它注射时

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');

然后从任何控制器可以访问/看它 users.current ,但你也可以设置它使用 this.users.set( 当前,NEWUSER)这将影响任何人看任何控制器或路线的属性。

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.

例如: 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.

这篇关于灰烬数据保存方法,创建VS更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆