uncaught typeerror: model.save() is not a function - Emberjs 1.13.8 - ember.js

I am new to ember.js, I am trying to save the model from another contoller where i have am able to get all the information of the model. Whenever i try to save the model using the code below
saveModel: function(model) {
model.save();
},
I am getting this error
Uncaught TypeError: model.save is not a function
Any idea how to solve this error?
Model information is getting passed from components using this.sendAction().

this.sendaction() should be CamelCase -> this.sendAction()
Check the Ember Guides for Components -> Handling Events to make sure you are using this.sendAction() correctly
Make sure by debugging that model is being passed correctly from the component.

Had the same error.
Using _internalModel works but is an ugly solution. My mistake was binding the model to the promise
this.set('prop', this.get('store').findRecord('model', id))
and not use
this.get('store').findRecord('user', this.selectedUser.id).then(function(model) {
self.set('prop', model);
});
Hope that this helps someone having the same problem.

Related

How to access model instance from function in model?

I wrote a function inside one of my Ember models that's designed to set some properties on the model instance. Below is a gross over-simplification of what the function is trying to do.
// In widget.js
export default DS.Model.extend({
enabled: DS.attr('boolean'),
...
turnOn() {
this.set('enabled', true);
}
});
I come from a Ruby background, so something like this feels very natural and appropriate, but this doesn't seem to work. I'm getting this error: Uncaught TypeError: Cannot read property 'set' of undefined. Am I doing something wrong? Is there a more appropriate way to accomplish what I'm trying to do?
Make sure in your controller/component action you are calling your model with this.get("some_model").turnOn()

Ember Data error when deleting model

I have an Ember-cli app (v2.1), and I'm having an issue with Ember Data when deleting a record from my blog model. Here's the code I'm calling for the deletion:
deleteBlog (blog) {
this.store.findRecord('blog', blog.get('id')).then(blog => {
blog.destroyRecord();
});
}
As expected, Ember Data makes the DELETE request, which works perfectly with my express.js backend service.
The only problem is that the record does not get removed from the Ember Data store, and I get the following error message:
Error: Attempted to handle event pushedData on my-app#model:blog::ember529:56455037f9cf29a325ae72b9 while in state root.deleted.inFlight
I appreciate any feedback!
The problem was in trying to select the model using an instance of the model that I already had. Changing it to a simple:
deleteBlog (blog) {
blog.destroyRecord();
}
got rid of the error! Thanks again to #torazaboro for helping me to take a closer look at the mistake.
Try this:
deleteBlog (blog) {
this.store.findRecord('blog', blog.get('id')).then(function(blog) {
blog.deleteRecord(); // => remove blog from the store
blog.save(); // => DELETE request
});
}
But this is supposed to do the same as blog.destroyRecord(); so it may not work.
What is probably happenning is that if you have a request to the server in-flight and you delete the model then you get an exception Attempted to handle event pushedData on model while in state root.deleted.uncommitted.

why does my emberjs model throw an exception when i try calling deleteRecord on it

I am doing this tutorial and just when i thought i got everything right, I came across this exception
JavaScript runtime error: Object doesn't support property or method 'deleteRecord'
in this context
removeTodo: function () {
var todo = this.get('model');
todo.deleteRecord();
todo.save();
}
i trying to follow the getting started tutorial on the Ember.js site.
It would appear that your model isn't actually a DS.Model (assuming that you're using Ember Data). Unless you're model is an actual record I'm ember, you won't be able to call delete record.

Emberjs - How to set model of different route when using named outlets

I'm loading data from XboxLive and other services in my ApplicationRoute and want to display that information using named outlets. I used this StackOverflow answer as a guide, but it's not working.
Fiddle is here: http://jsfiddle.net/sandalsoft/7xHfp/
In the ApplicationRoute, the model hook loads data from online services. This data is accessible as model.xbox, model.facebook, etc... setupController then sets the content of the XboxprofileController to model.xbox.
This doesn't work when I call App.Xboxprofile.find() (returns a promise), but it does work when I call App.Xboxprofile.findSimple() (returns a simple object). Why doesn't this work when find() has any kind of asynchrony? Am I missing something simple, or is this not the right way to architect this?
thanks
Thanks to #alexspeller in #emberjs for the answer.
I mistakenly thought model.xbox was a promise. It's an object. When I changed my model hook to use RSVP.hash() it worked perfectly:
model: function(controller)
var allModels = Em.RSVP.hash({
xbox: App.Xboxprofile.find('major nelson'),
}).then(function(hash) {
return Em.RSVP.hash(hash);
});
return allModels;
},

How to maintain data changes after a transaction is committed, including setting a belongsTo relationship

I am have difficulty setting a belongsTo relationship using ember data. I have a jsfiddle example to demonstrate. My original problem was that I was getting the following error:
Uncaught Error: <DS.StateManager:ember5035> could not respond to event loadedData in state rootState.loaded.updated.inFlight.
This only occurred when I updated the belongsTo relation on the model using the select. That was using Ember Data's built in RestAdapter. In the current example I am using the FixtureAdapter and I can't seem to persist any changes (if you check out the fiddle and edit the name of one the books, you will see that it reverts back to the original name)? They seem to revert as soon as the "server response" is simulated by the adapter? Is there a way to simulate a proper server response?
Also, I am wondering if my woes here are the result of how I've set up the data store transaction. I am using the following approach in the controller:
App.EditBookController = Ember.ObjectController.extend({
enterEditing: function() {
this.transaction = App.router.get('store').transaction();
this.transaction.add(this.get('content'));
},
updateRecord: function() {
this.transaction.commit();
this.transaction = null;
App.router.transitionTo('books');
}
});
Ok, you are using the very edge version of ember-data. This is great, but I have to put myself update too. With the help of #tchak, I can begin to make it working:
http://jsfiddle.net/Sly7/2XHZ2/45/
You will note I'm using App.Book.find() in order to load the fixtures for the first time, and App.Book.all() which seems to be a live Array, so things are kept updated.
I have to going further in order to make the select working.
Ok, my last try for the moment... http://jsfiddle.net/Sly7/2XHZ2/57/
LAST EDIT Got it working by adding the RESTSerializer to the FixtureAdapter. see http://jsfiddle.net/Sly7/2XHZ2/61/