Ember.js 如何加载模型并立即访问它
假设我加载页面时包含了当前用户信息的json哈希 在注入中,我加载以下信息:Ember.js 如何加载模型并立即访问它,ember.js,ember-data,ember-model,Ember.js,Ember Data,Ember Model,假设我加载页面时包含了当前用户信息的json哈希 在注入中,我加载以下信息: user_data = JSON.parse(user_json) App.Model.load(user_data['id'], user_data) container.typeInjection('controller', 'currentUser', 'controller:currentUser') App.set('currentUserController', controller) 但是我想在这里设置
user_data = JSON.parse(user_json)
App.Model.load(user_data['id'], user_data)
container.typeInjection('controller', 'currentUser', 'controller:currentUser')
App.set('currentUserController', controller)
但是我想在这里设置currentUserController的值-App.Model.load不会返回实际的模型实例
为了得到它,我需要运行App.Model.find(user_data['id']),因为这是在启动时完成的,所以ember Model似乎总是在数据库中查询这个模型,而不是使用我预加载的json
因为我在启动我的应用程序时使用了这个模型,所以我不能推迟加载-我如何才能在不需要ajax请求的情况下访问加载的模型?这个答案可能会帮助您: 至于从json加载,请尝试:
var store = DS.get("defaultStore");
var adapter = store.adapterForType(App.User);
var id = USER_ID_HERE;
adapter.didFindRecord(store, App.User, YOUR_JSON_DATA_HERE, id);
var user = App.User.find(id);
我使用的是余烬模型,所以余烬数据可能会有所不同 诀窍是使用create(而不是createrecord!)生成模型的空版本,然后用实例的load调用填充它。即
user = App.User.create()
user.load(user_data['id'], user_data)
这将把数据加载到您拥有的版本中,并正确地调用didLoad以确保它在存储中。谢谢-该链接回答了一个非常不同的问题。