Skip to content

Instantly share code, notes, and snippets.

@vincent178
Created July 30, 2014 08:03
Show Gist options
  • Save vincent178/f5afd6a7f7ea7cb085b8 to your computer and use it in GitHub Desktop.
Save vincent178/f5afd6a7f7ea7cb085b8 to your computer and use it in GitHub Desktop.
ember code snippet

Model

App.User = Ember.Object.extend({
    
    firstName: 'Hello',
    lastName: 'world',
    
    fullName: (function() { 
        console.log(this.get('firstName'));
        console.log(this.get('lastName'));
        return this.get('firstName') + " " + this.get('lastName'); 
        }
    ).property('firstName', 'lastName'),
    
    getFullName: function() {
        return this.get('firstName') + " " + this.get('lastName');   
    },
    
    showMessage: function(message) { alert(message) },
    
    showName: function() { this.showMessage(this.get('fullName')) },
    
    weightChanged: function() {
        if (this.get('weight') > 400) alert('yikes')
    }.observes('weight'),
    
    bodyObserver: function() {
        alert("You've changed. I feel like I don't even know you anymore.")
    }.observes('weight', 'height')
    
})

App.Human = Em.Object.extend();

App.Human.reopen({ name: 'Senor' });


App.Human.reopenClass( {
    sayUncle: function() { alert("uncle") }
})

Ember Object Flow

AboutRoute, AboutController, AboutView, and a template named about.js.emblem

Route

Route objects are really all about using hooks to prepare data and perform any setup actions required for the controller.

App.UsersRoute = Ember.Route.extend({

  beforeModel: function(transition) { },

  model: function(params, transition) { },

  afterModel: function(model, transition) { },

  activate: function() { },

  setupController: function(controller, model) {
    controller.set('model', model)
    // or this._super(arguments...)
  },

  deactivate: function() { }

})

the current model for that route this.modelFor('routeName')

get that controller object this.controllerFor('controllerName')

Sometimes you may want to get the model for that controller, in which case you would do this.controllerFor('controllerName').get('model')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment