- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 EmberJS 中,如果我想观察嵌套模型内的属性“selectedValue”,我该怎么做?
以下似乎不起作用;
modelChanged: function() {
}.observes('myModel.@each.@each.selectedValue'),
下面的方法也不起作用
modelChanged: function() {
}.observes('myModel'),
这就是 myModel 的样子
[
[{
"prop1": "abc_1",
"selectedValue": "abc_1"
}, {
"prop1": "xyz_1",
"selectedValue": "xyz_1"
}],
[{
"prop1": "abc_2",
"selectedValue": "abc_2"
}, {
"prop1": "xyz_2",
"selectedValue": "xyz_2"
}],
[{
"prop1": "abc_3",
"selectedValue": "abc_3"
}, {
"prop1": "xyz_1",
"selectedValue": "xyz_1"
}]
]
更新基于
似乎正在工作......只有几个问题;
In above code, "observedObjects" just adds for the first outer array and tracks property within this arr[0]. I have multiple outer array elements
How can I modify the above to track for multiple properties ? i.e. In some cases, it would be "selectedValue" OR it can be "preSelectedValue" in other cases . What property it is would depend upon that object itself. But I need to track change of either selectedValue/preSelectedValue
Also in observerMethod, can I get a context and check what the new selectedValue/preSelectedValue is?
最佳答案
您的嵌套模型的结构是什么?如果 myModel 是具有 selectedValue 的元素数组,则可以使用 myModel.@each.selectedValue
。如果您需要多层嵌套,则不支持开箱即用。
来自documentation :
Note that @each only works one level deep. You cannot use nested forms like todos.@each.owner.name or todos.@each.owner.@each.name.
有一些解决方法取决于您的模型结构,如果您用您的结构回复或更新问题,我将更新我的答案以涵盖适当的解决方案。
更新:根据 myModel
的设置/更新方式,您可能需要调整何时调用 observeMyModelChildren
以及它如何创建观察者。
// observerMethod is called when myModel.@each.@each.selectedValue changes
observerMethod: function () {
// handle change
},
// observedObjects tracks child observers so they can be removed
observedObjects: [],
// observeMyModelChildren listens for changes to myModel, removes
// old observers, and adds new ones
observeMyModelChildren: function () {
const key = '@each.selectedValue';
this.get('observedObjects').forEach((el) => {
el.removeObserver(key, this, this.observerMethod);
});
this.set('observedObjects', []);
let observedObjects = [];
this.get('myModel').forEach((el) => {
el.addObserver(key, this, this.observerMethod);
observedObjects.pushObject(el);
});
this.set('observedObjects', observedObjects);
}.observes('myModel'),
更新 2:如果您想支持观察多个属性(例如其他模型),可以通过修改 observeMyModelChildren
来实现:
observePropertyChildren: function (obj, attr) {
console.log('setting up observers on', attr);
var key = 'observedObjects.' + attr;
var observedObjects = this.get(key) || [];
observedObjects.forEach((el) => {
el.removeObserver('@each.selectedValue', this, this.observerMethod);
});
this.get(attr).forEach((el) => {
el.addObserver('@each.selectedValue', this, this.observerMethod);
observedObjects.pushObject(el);
});
this.set(key, observedObjects);
}.observes('myModel', 'myOtherModel'),
关于javascript - Ember 观察嵌套模型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39700712/
我正在尝试将 ember-models 链接到 ember-table,以从服务器提取分页记录,并在向下滚动时将它们添加到表中。 我可以通过请求我的 api url 和页码来让它工作,就像 http:
我有一个应用程序,它使用 ember-data 来持久化数据和 StateManager 来驱动其全局状态。由于加载数据是异步的,我想知道如何捕获表示所有数据都已加载的事件。 准确地说,我有一个名为“
我有一个简单的 Ember 应用程序,其中有一个输入框、两个选择框和一个按钮。我可以在“doSearch”方法中访问输入框的值,但不能访问选择框的值。 到目前为止,我没有尝试任何工作 - 我注释掉了我
我使用 Ember-CLI 来构建我的 ember 应用程序,但我不想使用 Ember Data。 默认情况下,当你输入这个命令:ember generate model Person 时,ember
我按照 Ember CLI 主页的说明创建了我的第一个 Ember CLI 插件。该插件运行良好,并且已经在 Github 上获得了几颗星:https://github.com/lolmaus/emb
我在这里看到的以前的问题似乎都没有涵盖何时使用 Ember 计算属性与 Ember Observer 的主题。我知道计算属性使用以前的属性来帮助生成新属性并在运行循环中更新。 Person = Emb
我一直在 git 分支上切换到 1.13.2 版本的 ember,现在我想在返回另一个分支时回到 1.11.1。 我一直在运行npm install和 bower install没有失败。 但是当我运
我有一个使用 ember-cli 构建的 Ember 应用程序我正在使用 ember-qunit 编写我的测试使用 testem 测试适配器并在浏览器中运行它们按照 ember-cli 中的说明文档。
项目设置: Ember:2.0.0 Ember 数据:2.0.0 Ember-cli:1.13.8 我创建了一个转换:转换/isodate.js import DS from 'ember-data'
假设我有一个具有 2 个属性的组件:我如何能够基于 localID 属性和 Ember.compulated.filterBy 宏创建计算属性? localID: 2, data: [ { i
我刚刚创建了一个新的 Ember-CLI app (v0.1.12),并注意到 Ember 的引用版本是 v1.8.1 (bower.json)。我想要 recently released 1.10我
使用最新的 Ember (3.2)、ember-cli-mirage 0.4.7、ember-cli-qunit 4.3.2、ember-qunit 3.4.1 我正在使用 ember-cli-mir
我是一名 ember 菜鸟,正在阅读 ember-cli 101 这本书。迄今为止,我一直在 ember-cli 网站 here 上使用升级工作流程。 。 在终端中使用 ember-cli 帮助时,我
如何测试我的组件,将 ember-data 模型作为 props 传递给它? 例如: {{#queue/review/moderatable-text model=activity property=
Ember 对象和 Ember Data 中的对象有什么区别?我知道当服务器上有一些数据时我应该使用 Ember Data 模型,但是我应该何时何地使用它们中的任何一个? 最佳答案 注意:这很长,有偏
执行ember -v仅显示ember cli版本。 如何查看 ember.js 版本和 ember 数据版本? 最佳答案 ember.js 和 ember-data 的版本由应用的依赖项决定。 Bow
我正在尝试在 Ember.RSVP.all 内执行 promise App.Foo = Ember.Object.create({ bar: function() { var
访问 ember 对象属性的首选方法是什么?我经常看到 .get() 方法被使用,但想知道为什么人们不只是使用点表示法。例如在 Advice in the Use of Ember Trek 先生写道
我知道 Ember 有一个记录器,但我想创建自己的用于学习目的。我有一个名为 logger 的服务,我希望能够在任何地方使用这个服务。将此服务注入(inject)到组件、 Controller 等中没
我刚刚关注了 ember-cli 主页上的“入门”帖子。 我安装了 ember 0.1.2、bower、phantomjs 等。 但是,我创建了我的应用程序,当我运行“ember server”时,它
我是一名优秀的程序员,十分优秀!