Save record of model is not working in ember-data 1.0.0-beta.3?

a 夏天 提交于 2019-12-24 16:05:06

问题


What I have done -

Model -

App.Book = DS.Model.extend({
   book_name: DS.attr('string'),
   edition: DS.attr('string')
});

Router -

App.Router.map(function() {
   this.resource('books', function() {
      this.route('new');
   });
});


App.BooksNewRoute = Ember.Route.extend({
    model: function() {
        return this.store.createRecord('book');
    },

    actions: {
       save: function() {
          this.modelFor('newBook').save();
       }
    }
});

Now Can anybody help me.. How to save data ?

I am getting error like

TypeError: this.modelFor(...) is undefined
this.modelFor('newBook').save();

回答1:


It is hard to tell how the context of your action looks like.

But one option is to pass the object you want to save as a parameter like this {{action save myBook}}.

Then you action could look like this:

App.BooksNewRoute = Ember.Route.extend({
    model: function() {
        return this.store.createRecord('book');
    },

    actions: {
       save: function(book) {
          book.save();
       }
    }
});



回答2:


I think this should work.

App.BooksNewRoute = Ember.Route.extend({
    model: function() {
        return this.store.createRecord('book');
    },

    actions: {
       save: function() {
          this.get('model').save();  
       }
    }
});


来源:https://stackoverflow.com/questions/19402943/save-record-of-model-is-not-working-in-ember-data-1-0-0-beta-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!