Book Image

Learning Yeoman

By : Jonathan Spratley
Book Image

Learning Yeoman

By: Jonathan Spratley

Overview of this book

Table of Contents (17 chapters)
Learning Yeoman
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Modern Workflows for Modern Webapps
Index

Controllers


In Ember.js, controllers allow you to decorate your models with display logic; models have properties that are sent to the server and controllers have properties that do not need to be sent to the server.

Post edit controller

The generator will create a controller that will control the edit view; let's add some logic that will handle saving and destroying a model.

Open the app/scripts/controllers/post_edit_controller.coffee file and add the following content:

LearningYeomanCh5.PostEditController = Ember.ObjectController.extend(
  needs: 'post'
  actions:
    save: ->
      @get('model').save()
      @transitionToRoute 'post', @get('model')
    destroy: ->
      @get('model').deleteRecord()
      @transitionToRoute 'posts'
)

The preceding code does the following:

  • The PostEditController class is defined extending the Ember.ObjectController class

  • The needs property specifies that this controller depends on the post controller

  • The actions object declares a save action and a destroy...