EmberData peekRecord()方法

时间:2017-09-20 06:57:47

标签: ember.js ember-data

我一直在组合的应用程序看起来像商店列表(带有添加/编辑/删除选项),点击商店名称会将您带到该商店中的商品列表(再次添加/编辑/删除)。 路由器:

Router.map(function() {
    this.route('about');
    this.route('stores');
    this.route('shop', function() {
        this.route('items', { path: '/:shop_id/items' });
        this.route('edit', { path: '/:shop_id/edit' });
    });
});

模特:

// app/models/shop.js
import DS from 'ember-data';

export default DS.Model.extend({
  shopName: DS.attr(),
  shopDetails: DS.attr(),
  items: DS.hasMany('item')
});

// app/models/item.js
import DS from 'ember-data';

export default DS.Model.extend({
  itemName: DS.attr(),
  itemDetails: DS.attr(),
  itemPrice: DS.attr(),
  parentShop: DS.belongsTo('shop')
});

显示项目列表的页面的路线是:

// app/routes/shop/items.js
import Ember from 'ember';

export default Ember.Route.extend({
    model(params) {
        return this.get('store').findRecord('shop', params.shop_id, {
            include: 'items'
        });
    }
});

据我所知,在向item添加shop之前,我必须致电peekRecord()

// app/controllers/shop/items.js
actions: {
    saveItem() {
        let currentShop = this.get('store').peekRecord('shop', /* shop id here */);
        //rest of the code
    }
}

从:

// app/templates/shop/items.hbs
<button type="submit" {{action 'saveItem'}}>Save</button>

问题是,如何将商店ID作为peekRecord()的第二个参数传递?

1 个答案:

答案 0 :(得分:1)

想通了。

// app/templates/shop/items.hbs
<button type="submit" {{action 'saveItem' model}}>Save</button>

// app/controllers/shop/items.js
actions: {
    saveItem(param) {
        let currentShop = this.get('store').peekRecord('shop', param.id);
        //rest of the code
    }
}