1

I am trying to use the new ember-data syntax like explained here: https://github.com/emberjs/data/blob/master/TRANSITION.md (read from Transaction is Gone: Save Individual Records ).

When I hit the save button I get the error Uncaught TypeError: Cannot call method 'save' of undefined in the console. Also in the network tab, there is no POST request to the api.

The template

<script type="text/x-handlebars" data-template-name="landcode/new">
    Code: {{input value=code}}<br />
    Image: {{input value=image}}<br />
<button {{action 'saveLandcode'}}>opslaan</button>

The app.js (relevant code)

App.Router.map(function() {
    this.resource("landcodes"),
    this.resource("landcode", function() {
        this.route("new");
    });
});

App.LandcodeNewRoute = Ember.Route.extend({
    model: function() {
        this.store.createRecord('landcode');
    },
    actions: {
        saveLandcode: function(){
            this.modelFor('landcode').save(); // does not save
        }
    }
});

App.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: 'api'
});
App.Store = DS.Store.extend({
    adapter: 'App.ApplicationAdapter'
});

App.Landcode = DS.Model.extend({
    code: DS.attr('string'),
    image: DS.attr('string')
});

2 Answers 2

2

You are using this.modelFor('landcode') this will take the returned model from App.LandcodeRoute, but your model is returned from LandcodeNewRoute. Just use this.currentModel, since you want the model of the current route.

App.LandcodeNewRoute = Ember.Route.extend({
    model: function() {
        return this.store.createRecord('landcode');
    },
    actions: {
        saveLandcode: function(){
            this.currentModel.save();
        }
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Both thanks for the answer. However both solutions give the same error as before. I go to setup a jsFiddle, then it will be clear to everyone what is going on.
I saw in your code that you are missing the return in this.store.createRecord ... Did you update your current implementation?
Hi Márcio, thanks for that last comment! Indead, I had missed that return statement you added. I removed my last JS Bin comment, setting up a Ember.js app on jsFiddle or JS Bin is a very unpleasant activity by the way. Now we have found the bug, thanks again!
2

Your model for should include the route name as well

App.LandcodeNewRoute = Ember.Route.extend({
    model: function() {
        return this.store.createRecord('landcode');
    },
    actions: {
        saveLandcode: function(){
            this.modelFor('landcode.new').save(); // the correct model
        }
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.