public
Last active

  • Download Gist
application.handlebars
Handlebars
1 2
{{#linkTo 'firms.new'}}New{/linkTo}}
{{outlet}}
firms.handlebars
Handlebars
1
{{outlet}}
firms.new.handlebars
Handlebars
1 2 3 4 5 6 7 8
<form>
{{view Ember.TextField valueBinding="name"}}
{{#if errors.name}}
<span>{{errors.name}}</span>
{{/if}}
<button {{action 'save'}}>Ok</button>
<button {{action 'cancel'}}>Cancel</button>
</form>
firms_new_controller.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
App.FirmsNewController = Ember.ObjectController.extend({
needs: ['currentUser', 'currentFirm'],
errors: null,
 
createModel: function(attrributes) {
return this.get('store').transaction().createRecord(App.Firm, attrributes);
},
 
subscribeSave: function() {
this.get('content').on('didCreate', this, function() {
return this.transitionToRoute('index');
});
 
this.get('content').on('becameInvalid', this, function() {
this.handleFailure();
});
 
this.get('content').on('becameError', this, function() {
this.handleFailure();
});
},
 
handleFailure: function() {
this.set('errors', this.get('content.errors'));
this.get('transaction').rollback();
this.set('content', this.createModel(this.get('toJSON')));
this.subscribeSave();
},
 
save: function() {
var user = this.get('controllers.currentUser.content');
this.get('users').addObject(user);
 
if (this.get('isValid') && !this.get('isSaving')) {
this.get('transaction').commit();
}
},
 
cancel: function() {
this.transitionToRoute('index');
}
});
firms_new_route.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14
App.FirmsNewRoute = Ember.Route.extend({
model: function() {
return this.controllerFor('firmsNew').createModel();
},
 
setupController: function(controller, model) {
controller.set('errors', null);
controller.subscribeSave();
},
 
deactivate: function() {
this.controllerFor('firmsNew').get("transaction").rollback();
}
});
gistfile1.txt
1 2 3 4 5
DEBUG: -------------------------------
DEBUG: Ember.VERSION : 1.0.0-rc.3
DEBUG: Handlebars.VERSION : 1.0.0-rc.3
DEBUG: jQuery.VERSION : 1.9.1
DEBUG: -------------------------------
router.js
JavaScript
1 2 3 4 5
App.Router.map(function() {
this.resource('firms', function() {
this.route('new');
});
});
store.js
JavaScript
1 2 3 4 5 6
App.Store = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.extend({
namespace: 'api/v1'
})
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.