Skip to content

Instantly share code, notes, and snippets.

@stevenchanin
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stevenchanin/fd23d5495ac0a697fb72 to your computer and use it in GitHub Desktop.
Save stevenchanin/fd23d5495ac0a697fb72 to your computer and use it in GitHub Desktop.
### Router.js.coffee
App.Router.reopen
rootURL: '/'
location: 'auto'
App.Router.map ->
@resource 'users', ->
@resource 'user', path: '/:id', ->
@route 'edit'
@resource 'clients', ->
@resource 'client', path: '/:id', ->
@route 'edit'
### Models/clients.js.coffee
App.Client = DS.Model.extend
name: DS.attr('string')
active: DS.attr('boolean')
owner: DS.belongsTo('user')
### Models/users.js.coffee
App.User = DS.Model.extend
firstName: DS.attr('string')
lastName: DS.attr('string')
email: DS.attr('string')
clients: DS.hasMany('client')
fullName: ( ->
@get('firstName') + ' ' + @get('lastName')
).property('firstName', 'lastName')
### Router/clients.js.coffee
App.ClientsRoute = Ember.Route.extend
model: -> @store.find 'client'
### Router/client.js.coffee
App.ClientRoute = Ember.Route.extend
model: (params) -> @store.find 'client', params.id
### Router/client_edit.js.coffee
App.ClientEditRoute = Ember.Route.extend
activate: -> @controllerFor('client').set('isEditing', true)
deactivate: -> @controllerFor('client').set('isEditing', false)
setupController: (controller, model) ->
@_super(arguments...)
controller.set('owners', @store.find 'user')
### Controller/clients.js.coffee
App.ClientsController = Ember.ArrayController.extend
sortProperties: ['name']
clients: ( ->
if @get('search') then @get('searchedClients') else @
).property('search', 'searchedClients')
searchedClients: ( ->
search = @get('search').toLowerCase()
@filter (client) => client.get('name').toLowerCase().indexOf(search) != -1
).property('search', '@each.name')
### Controller/client.js.coffee
App.ClientController = Ember.ObjectController.extend
isEditing: false
### Controller/client_edit.js.coffee
App.ClientEditController = Ember.ObjectController.extend
actions:
saveChanges: ->
@get('model').save().then =>
@transitionToRoute('client')
cancel: ->
@get('model').rollback()
@transitionToRoute('client')
### template/client.js.emblem
article#client
h2= model.name
if isEditing
outlet
else
dl.dl-horizontal
dt Name
dd= model.name
dt Active
dd= model.active
dt Owner
dd= model.owner.fullName
dt  
dd= link-to 'edit' 'client.edit' model class="edit"
### templates/client/edit.js.emblem
form role="form"
.form-group
label Name:
view Ember.TextField value=model.name class="form-control"
.form-group
label Active:
view Ember.Checkbox value=model.active class="form-control"
.form-group
label Owner:
log model
log model.owner
log model.owner.id
view Ember.Select content=owners optionValuePath="content.id" optionLabelPath="content.fullName" value=owner.id class="form-control"
/ .form-group.text-right
/ input type="submit" value="Save Changes" click="saveChanges" class="btn btn-default"
/ '
/ a.cancel href='#' click="cancel" cancel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment