Frage

It seems that Spine's Model.updateAttributes only updates attributes, and does not create new ones in case you supply any.

In my usecase, I have a controller that creates part of the attributes. Then through an Ajax request the server responds with the full object, and I want to update the model instance living in Spine with the additional variables.

For example, I have a model with attributes: name, date_created. Through the controller a user instantiates an object providing only the name. An Ajax request notifies the server which in turn responds with a name and a date_created. This date_created should then be added to the user's model.

Model.updateAttributes doesn't work, and I wouldn't be too fond of deleting the object and creating a new one - that just seems as too much overhead. I could provide default values for variables that are not set upon creation, but that also has a negative side. I guess what I'm looking for is a method that could be called Model.createOrUpdateAttributes. Can anybody recommend a way to achieve this? Thanks!

War es hilfreich?

Lösung

I might haven't fully understood your usecase, but I'll try to answer. You need to declare whatever attributes a type of a model has with the configure class method. This declaration helps various model function to do their job later. After you declare all the attributes you need, you can create model instances with any of the previously declared attributes. You don't have to provide values for all the declared attributes. After the ajax call returns, the date_created will be set on your model instance. Until this happens it will be just undefined.

If this solution still can't work for you, please describe why, and I'll gladly try to help.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top