Ember文档说要定义这样的商店
MyApp.Store = DS.Store.extend();
如果您要查找组件中的记录,此文档说您可以像这样将商店注入组件中
// inject the store into all components
App.inject('component', 'store', 'store:main');
但是,我使用的是这样定义的本地存储适配器
App.ApplicationAdapter = DS.LSAdapter.extend({namespace:'my-namespace'});
因此,我不知道如何按照上述说明将其注入到组件中(我需要在其中查找记录)。
按照此SO答案的说明,我尝试store
通过像store=store
和/或一样将其传递到组件中store=controller.store
<li> {{my-component id=item.customid data=item.stats notes=item.notes store=store}} </li>
要么
<li> {{my-component id=item.customid data=item.stats notes=item.notes store=controller.store}} </li>
然后的目标是能够在竞争对手的行动中做到这一点
var todo = this.get('store');
console.log(todo, "the new store");
todo.set('notes', bufferedTitle);
console.log(todo, "todo with notes set");
todo.save();
但是,todo.save();
总是触发
Uncaught TypeError: undefined is not a function
注意我登录了商店吗?这就是它所显示的
Class {_backburner: Backburner, typeMaps: Object, recordArrayManager: Class, _pendingSave: Array[0], _pendingFetch: ember$data$lib$system$map$$Map…}
如果我检查它(通过打开树,此处未显示),它确实表明注释是通过设置的,todo.set('notes', bufferedTitle);
但是它没有我为索引路由定义的模型的其他任何属性,而且该对象没有“保存”方法。因此,它似乎不是实际的存储,而只是一些backburner对象。
我尝试此SO答案得到相同的结果,它说要获取targetObject的存储
var todo = this.get('targetObject.store');
注意,我也尝试过此操作,即将商店设置为商品的商店。
<li> {{my-component id=item.customid data=item.stats notes=item.notes store=item.store}} </li>
应该注意的是,如果我在组件中设置商店,则可以通过执行{{store}}
此操作在页面上打印商店
<DS.Store:ember480>
但var todo = this.get('store');
即使在应用程序代码中,我也无法执行处理点击的操作。
问题,使用localStorage适配器,我如何在组件中查找记录(目的是能够更改记录然后再次保存)
请注意,如果很重要,我会像这样为(索引)路由定义一个模型
App.Index = DS.Model.extend({
title: DS.attr('string'),
版本(不幸的是,我不知道我使用的是哪个版本的Ember数据或适配器)
Ember Inspector
1.7.0
Ember
1.9.1
Ember Data
<%= versionStamp %>
Handlebars
2.0.0
jQuery
1.10.2
更新以响应更多信息的请求
设置问题的代码非常简单。这是路由器(对资源使用错误的名称:) App.Router.map(function(){this.resource('index',{path:'/'});}
这是获取要在索引路由中使用的记录的路由
App.IndexRoute = Ember.Route.extend({
model: function{
var resource = this.store.find('index');
return resource;
}
});
我有一个Index Controller,它对组件没有任何作用(除非我应该在Controller上定义由组件事件触发的方法)
在html中,我使用把手将数据传递到组件
{{#each item in items}}
<li> {{my-component id=item.customid data=item.stats notes=item.notes store=store}}
{{/each}}
然后,在中components/my-component
,我有一个标签,该标签应在单击时触发一个动作,该动作将允许我编辑模型上的属性之一
<label> {{action "editTodo" on="doubleClick">{{notes}}</label>
单击将触发中的此代码App.MyComponent
,从而触发提示此问题的错误
var todo = this.get('store')
todo.set('notes', bufferedTitle);
todo.save()
恕我直言,将其store
注入组件不是最好的主意...根据设计,组件应该是隔离的,并且不应该对组件有任何了解store
。
在给出的文档中,它写为:通常,直接在组件中查找模型是一种反模式,您应该更喜欢在包含组件的模板中传入所需的任何模型。
但是,如果由于某种原因确实需要它,那么为什么不仅仅将变量传递store
给组件呢?
{{my-component store=store}}
然后,您只能store
从控制器中真正需要的组件中传递。
store
在所有组件中注入,很可能会导致您设计不佳(尽管乍一看似乎很诱人)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句