lundi 23 mars 2015

How to use a blank Ember Data model in a route?

I haven't been able to find much of anything on the following, any posts or guides you may have on this would be appreciated!


I am trying to create a new record with a form and router. The way that I am going about this is by setting the model in the router, then saving the record when the user submits the form with an action.


I have seen other examples where the author does a "createRecord" in the controller and grabs the values from the form with "get" or "getProperties". However, in my code below it seems this is much simpler:



Dashboard.CreateAccountRoute = Ember.Route.extend({
model: function() {
return this.store.createRecord('account', {});
},
actions: {
createAccount: function () {
var account = this.modelFor('create_account');

var self = this;
account.save().then(function(account){
self.flashMessage('The account was added successfully!', 'alert-success');
self.transitionTo("accounts");
}, function(reason) {
self.flashMessage('There was an error saving the account. Please try again.', 'alert-error');
console.log(reason);
});
}
}
});


The problem with this of course, is if the user does not submit the form, the record is still added to the local store. Otherwise, it works as expected setting values in the record from the form.


How can I accomplish the above without saving anything to the store until after the action "createAccount"?





Aucun commentaire:

Enregistrer un commentaire