Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ember.js

 Ember.js

galfert

May 19, 2012
Tweet

Other Decks in Programming

Transcript

  1. Example = Ember.Application.create(); Example.person = Ember.Object.create({ name: 'John Doe' });

    Example.car = Ember.Object.create({ ownerBinding: 'Example.person.name' }); console.log(Example.car.owner); > "John Doe" Example.person.set('name', 'Jane Doe'); console.log(Example.car.owner); > "Jane Doe"
  2. Example.person = Ember.Object.create({ firstName: 'John', lastName: 'Doe', name: function() {

    return this.get('firstName') + ' ' + this.get('lastName'); // Tell Ember that this computed property depends on // firstName and lastName }.property('firstName', 'lastName') });
  3. Example.person = Ember.Object.create({ firstName: 'John', lastName: 'Doe', name: function(key, value)

    { // getter if (arguments.length === 1) { var firstName = this.get('firstName'); var lastName = this.get('lastName'); return firstName + ' ' + lastName; // setter } else { var name = value.split(' '); this.set('firstName', name[0]); this.set('lastName', name[1]); return value; } }.property('firstName', 'lastName') });
  4. <script type="text/x-handlebars"> {{#view Example.NewCommentView}} {{view Ember.TextField viewName="name" placeholder="Your name"}} {{view

    Ember.TextField viewName="email" placeholder="Your email"}} {{view Ember.TextArea viewName="text" placeholder="Your comment"}} <button {{action "create"}}>Submit</button> {{/view}} </script>
  5. Example.NewCommentView = Ember.View.extend({ tagName: 'form', commentsBinding: 'Example.commentsController', create: function() {

    this.get('comments').createComment( this.getPath('name.value'), this.getPath('email.value'), this.getPath('text.value') ); } });
  6. Example.commentsController = Ember.ArrayController.create({ content: [], createComment: function(name, email, text) {

    var comment = Example.Comment.create({ name: name, email: email, text: text }); this.pushObject(comment); } });
  7. App.stateManager = Ember.StateManager.create({ start: Ember.State.extend({ index: Ember.State.extend({ route "/", setupContext:

    function(manager) { manager.transitionTo('posts.index') } }), posts: Ember.State.extend({ route: "/posts", setupContext: function(manager) { // the postsController is shared between the index and // show views, so set it up here var postsController = manager.get('postsController'); postsController.set('content', Post.findAll()); }, // [...]
  8. // [...] index: Ember.State.extend({ route: "/", setupContext: function(manager) { //

    the application's main view is a ContainerView whose // view is bound to the manager's `currentView` manager.set('currentView', App.PostsView.create({ controller: postsController })); }, showPost: function(manager, event) { // this event was triggered from an {{action}} inside of // an {{#each}} loop in a template manager.transitionTo('show', event.context); } }), // [...]
  9. // [...] show: Ember.State.extend({ route: "/:post_id", setupContext: function(manager, post) {

    var postsController = manager.get('postsController'); postsController.set('selected', post); manager.set('currentView', App.PostView.create({ controller: postController })) }, serialize: function(manager, post) { return { "post_id": post.get('id') } }, deserialize: function(manager, params) { return Post.find(params["post_id"]); } }) // [...]
  10. App.store = DS.Store.create({ revision: 4, adapter: DS.RESTAdapter.create({ namespace: 'api' })

    }); App.Person = DS.Model.extend({ firstName: DS.attr('string'), lastName: DS.attr('string'), birthday: DS.attr('date'), fullName: function() { return this.get('firstName') + ' ' + this.get('lastName'); }.property('firstName', 'lastName') });
  11. CUSTOM ATTRIBUTE TYPE DS.attr.transforms.boolString = { from: function(serialized) { if

    (serialized === 'Y') { return true; } return false; }, to: function(deserialized) { if (deserialized) { return 'Y'; } return 'N'; } }
  12. ASSOCIATIONS App.Profile = DS.Model.extend({ about: DS.attr('string'), postCount: DS.attr('number') }); App.Author

    = DS.Model.extend({ profile: DS.hasOne('App.Profile'), name: DS.attr('string') }); var author = App.store.find(App.Author, 1); author.get('name'); author.get('profile'); author.getPath('profile.postCount');
  13. RETRIEVE • find • findMany • findAll • filter var

    oldPeople = App.store.filter(App.Person, function(data) { if (data.age > 80) { return true; } });
  14. CREATE, UPDATE, DELETE • createRecord • deleteRecord var john =

    App.store.createRecord(App.Person, { name: 'John Doe' }); App.store.commit(); john.set('name', 'John Wayne'); App.store.commit(); john.deleteRecord(); App.store.commit();