Skip to content

Instantly share code, notes, and snippets.

@wycats
Created December 3, 2012 16:07
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wycats/4195967 to your computer and use it in GitHub Desktop.
Save wycats/4195967 to your computer and use it in GitHub Desktop.

Basic Strategy

Take a match block and add routes to a RouteRecognizer:

match("/posts").to("posts", function(match) {
  match("/").to("postIndex")
  match("/:id").to("showPost");
  match("/edit").to("editPost");
  match("/new").to("newPost");
});

match("/posts").to("postIndex");

match("/posts", function(match) {
  match("/").to("postIndex");
})


// generate

router.add([{ path: "/posts", handler: "posts" }, { path: "/:id", handler: "showPost" }], { as: "posts.show" });
router.add([{ path: "/posts", handler: "posts" }, { path: "/edit", handler: "editPost" }]);
router.add([{ path: "/posts", handler: "posts" }, { path: "/new", handler: "newPost" }]);

router.generate("posts.show", { id: 1 }) // /posts/1
{{ linkTo "showPost" post }}

Route Handlers

App.ShowPostRoute = Ember.RouteHandler.extend({
  serialize: function(post) {
    return { id: post.get('id'); }
  },

  contentForController: function(params) {
    return App.Post.find(params.id);
  },

  setupControllers: function(post) {
    this.set('controller.content', post);
  },

  renderTemplates: function() {
    this.render('show_post', { into: 'application', outlet: 'view', controller: this.get('controller'), view: App.ShowPostView })
  },

  events: {
    deletePost: function(post) {
      var postController = this.controllerFor('post');

      post.deleteRecord();
    
      this.transitionTo('postIndex', posts);
    }
});

App.PostIndexRoute = Ember.RouteHandler.extend({
  contentForController: function() {
    return App.Post.findAll();
  },

  setupControllers: function(posts) {
    this.set('controller.content', posts);
  }
})

States-Without-URLs

App.ShowPostRoute = Ember.RouteHandler.extend({
  initialSubstate: 'showingComments',
  
  enter: function() {
    this.transitionTo('showingComments');
  },
 
  substates: {
    showingComments: Ember.State.extend({
      renderTemplates: function() {
      
      }
    }),
    
    showingTrackbacks: Ember.Handler.extend({
    
    })
  }
});


match("/login").to("login");
match("/").to("dashboard");

App.LoginState = Ember.State.extend({
  renderTemplates: function() {
    this.render('login')
  }
});

App.LoggingInState = Ember.State.extend({
  renderTemplates: function() {
    this.render('loggingIn');
    
    // or do nothing, and use the loginController to control the login UI
  },

  events: {
    loggedIn: function() {
      this.transitionTo("dashboard");
    },
        
    failure: function() {
      this.controllerFor("login").set("failure", true);
      this.transitionTo("login");
    }
  }
});

App.LoginHandler = Ember.RouteHandler.extend({
  substates: {
    login: App.LoginState,
    loggingIn: App.LoggingInState
  }
});
@logicalhan
Copy link

Under the heading Route Handlers, how are you accessing the posts variable in the deletePost function?

 events: {
    deletePost: function(post) {
      var postController = this.controllerFor('post');

      post.deleteRecord();

      this.transitionTo('postIndex', posts);
    }
  }

@wycats btw, thank you for all the open-source contributions. i've been a beneficiary of your work for some time now, and I just wanted to express my gratitude for it.

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