Ember数据查找与负载

时间:2012-05-09 11:09:50

标签: ember.js ember-data

当你在ember-data中使用findQuery时,它是否也加载模型localy?我无法使用以下代码:

App.MyModel = DS.Model.extend {

   name: DS.attr('string')
   didLoad: ->
    console.log('model loaded')
}

现在当我做类似的事情时:

objects = App.store.find(App.MyModel, [{name: "john"},{name: "jack"}])

不会触发didLoad回调。何时触发此回调?

1 个答案:

答案 0 :(得分:1)

要实现查询功能,您必须在适配器中实现findQuery方法。此方法需要4个参数store, type, query, modelArray。当服务器返回查询的数据时,您必须在load上调用modelArray方法以使用查询结果填充它。此方法还会将数据加载到商店中,请参阅此处的示例:http://jsfiddle.net/pangratz666/5HMGd/

App.store = DS.Store.create({
    revision: 4,
    adapter: DS.Adapter.create({
        find: Ember.K,
        findQuery: function(store, type, query, modelArray) {
            // expect server to return this array
            modelArray.load([{ id: 1, name: 'John'}, { id: 2, name: 'Jack'}]);
        }
    })
});

App.MyModel = DS.Model.extend({
    name: DS.attr('string'),
    didLoad: function() {
        console.log('model loaded', this.toJSON());
    }
});

// invoke query which loads the 2 models, and didLoad is called
App.store.find(App.MyModel, {});