Created
February 2, 2014 20:44
-
-
Save iamdustan/8774653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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