Ember 2,来自组件my-modal.hbs的model.save()

时间:2015-09-30 15:45:32

标签: javascript ember.js ember-data ember-cli ember-2.0.0

我有 posts.hbs

{{#each model as |post|}}
    <a href="#" {{action 'openWriteModal' post}}>

      <h3>{{post.title}}</h3>
          {{post.text}}
          {{post.author.name}}
    </a>
{{/each}}

然后我打开 write-modal.hbs

{{input value=model.title size="40" escape-press='close'}}

{{model.author.name}}

{{input value=model.text size="70" escape-press='close'}}

<button {{action 'close'}}>Close</button>
<button {{action 'save' model}}>Save</button>

现在,这是我的 components / write-modal.js

export default Ember.Component.extend({

  actions: {

    close: function() {
      return this.sendAction('close');
    },

    save(model) {
        model.set("text", model.get("text"));
        model.save().then(this.sendAction('close'));
    }
  }
});

这里是 posts.js

export default Ember.Route.extend({

    model() {
        return this.store.findAll('post');
    },

  actions: {
    openWriteModal(post) {

      return this.render('modal', {
        outlet: 'modal',
        into: 'application',
        model: this.store.findRecord('post', post.id, { reload: true }),
        controller: 'application'
      });
    },

    closeWriteModal() {

      return this.disconnectOutlet({
        outlet: 'modal',
        parentView: 'application'
      });
    },

    saveWriteModal(model) {

      model.save();
},

......

  model(params) {
    return this.store.query('post', params);
  }

});

在我的 application.hbs

{{outlet}}

{{outlet "modal"}}

最后 modal.hbs

{{write-modal model=model close='closeWriteModal' save='saveWriteModal'}}

但是我收到了这个错误:“Uncaught TypeError:无法读取未定义的属性'save'”

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您正在发送&#34; POST&#34;记录为参数

{{#each model as |post|}}
    <a href="#" {{action 'openWriteModal' "----post----"}}>  

      <h3>{{post.title}}</h3>
          {{post.text}}
          {{post.author.name}}
    </a>
{{/each}}

这是一个余烬记录。然后你试图找到完全相同的POST

而不是使用(你需要指定&#34;目录/ model_name&#34;不是DS记录实例)

 model: this.store.findRecord('post', post.id, { reload: true }), 

使用

return this.render('modal', {
    outlet: 'modal',
    into: 'application',
    model: post
    controller: 'application'
  });

不确定这是否有助于解决您的问题,请与我们联系。