【问题标题】:Error saving object in ember.js: 'Object X has no method 'save''在 ember.js 中保存对象时出错:'对象 X 没有方法'保存''
【发布时间】:2013-11-21 02:24:23
【问题描述】:

我正在使用 Rails 和 Ember.js 构建应用程序,但在创建新对象时遇到了问题。在我的 ember 控制器中,当调用保存操作时,它会尝试调用 this.get('model').save()。实际对象只需要创建一个称为“内容”的参数。我得到的错误是Uncaught TypeError: Object xstringx has no method 'save',其中xstringx 是我在内容文本区域框中输入的实际值。

this.get('model') 实际上是返回一个字符串!我怎样才能让它返回我的模型?以下是一些相关代码:

路线:

App.MessagesNewRoute = Ember.Route.extend({
model: function(){
    this.store.createRecord('message');
},
setupController: function(controller, model){
    controller.set('content', model);
},
renderTemplate: function() {
    this.render('app/templates/messages/new');
}
});

控制器:

App.MessagesNewController = Ember.ObjectController.extend({
actions: {
    save: function(){
    return this.get('model').save().then(function(){
        return _this.transitionToRoute('messages.index');
    });
    }       
}

});

查看:

App.MessagesNewView = Ember.View.extend({
templateName: "app/templates/messages/new",
actions: {
    save: function() {
        console.log(arguments);
    }
}
});

新消息模板:

<form role="form">
 <fieldset>
 <div class="form-group">
   <label {{bindAttr for="contentField.field_id"}}>Content</label>
  {{view Ember.TextArea valueBinding='content' class='form-control' name='content' viewName='contentField'}}
</div>
<div class="btn-group">
    <button type="submit" {{action "save"}} class="btn btn-success">Submit</button>
    <button class="btn btn-dancer">Cancel</button>
</div>

消息模型:

App.Message = DS.Model.extend({
content: DS.attr('string'),
user_id: DS.attr('number'),
created_at: DS.attr('string'),
updated_at: DS.attr('string'),
user: DS.belongsTo('user', {async: true}),
priority: DS.attr('number'),
priorityClass: function(user_id){
     //Return the middle size until implemented
    return 'importance-medium';
}.property('priority')
});

【问题讨论】:

  • 你能展示你的消息模型吗?
  • @MárcioRodriguesCorreaJúnior 查看我的编辑。
  • 当您使用内容时,该属性解析为整个对象,而不是模型的 content 属性,更新为 content.content 或 model.content,如 @michael-johnston 所述,并且将起作用.
  • @MárcioRodriguesCorreaJúnior,绑定到 content.content 和 model.content 会导致未定义的错误...
  • 但不推荐使用model.content,content 已弃用。 ;)

标签: javascript ruby-on-rails ember.js ember-data


【解决方案1】:

不确定是否是拼写错误,但您需要从模型方法中返回数据:

App.MessagesNewRoute = Ember.Route.extend({
    model: function(){
        return this.store.createRecord('message');
    },
    setupController: function(controller, model){
        controller.set('content', model);
    },
    renderTemplate: function() {
        this.render('app/templates/messages/new');
    }
});

并使用 content.content 或 model.content 就像迈克尔描述的那样

{{view Ember.TextArea valueBinding='model.content' class='form-control' name='content' viewName='contentField'}}

【讨论】:

  • 谢谢!缺少的 return 语句结合 model.content 解决了这个问题。看起来我从一个示例中复制了一些咖啡脚本并忘记添加返回。
【解决方案2】:

出于遗留原因,内容是 Ember 中的保留字。

尝试将您的 textArea 绑定到 model.content,或将属性名称更改为其他名称。

{{view Ember.TextArea valueBinding='model.content' class='form-control' name='content' viewName='contentField'}}

【讨论】:

  • 我现在得到:Uncaught TypeError: Cannot call method 'save' of undefined
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多