Skip to content

Instantly share code, notes, and snippets.

@iamdustan
Created February 2, 2014 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamdustan/8774653 to your computer and use it in GitHub Desktop.
Save iamdustan/8774653 to your computer and use it in GitHub Desktop.
App = Ember.Application.create();
App.ApplicationAdapter = DS.LSAdapter.extend({
namespace: 'lendly'
});
App.Router.map(function() {
this.resource('index', { path: '/' }, function() {
this.resource('item', { path: '/:item_id'}),
this.resource('new', { path: '/new'})
});
});
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.find('item');
}
});
App.ItemRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('item', params.item_id);
}
});
App.ItemController = Ember.ObjectController.extend({
actions: {
deleteItem: function() {
var item = this.get('model');
item.deleteRecord();
item.save();
this.transitionToRoute('index');
}
}
});
App.NewController = Ember.ArrayController.extend({
actions: {
createItem: function() {
App.Utils.saveAndClearModel.call(this, 'item', ['title', 'lentTo', 'notes' ]);
this.transitionToRoute('item', item);
}
}
});
App.Item = DS.Model.extend({
title: DS.attr('string'),
lentTo: DS.attr('string'),
notes: DS.attr('string'),
isReturned: DS.attr('boolean')
});
App.Utils = {
saveAndClearModel: function(recordName, properties) {
var self = this;
// create an object with all properties
var props = properties.reduce(function(obj, prop) {
obj[prop] = self.get(prop);
return obj;
}, {});
var record = this.store.createRecord(recordName, props);
record.save()
// clear all properties
properties.forEach(function(prop) {
self.set(prop, '');
});
return record;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment