Hello Octopress

真的是好久沒寫blog了。看了一下最後一篇是在3月2日已經整整4個月沒寫了。

markdown的語法也都快忘光了:(一開始執行rake new_post時還發生錯誤無法產生新文章,快速地google了一下原來是dependency太舊的關係,執行完bundle update就OK了真是好險啊。 前陣子使用emberjs發現了一些之前沒注意到的細節。

使用transitionTo不會呼叫在route裡的model function

例如底下的程式碼,當使用transitionTo()到post route的時候. emberjs並不會幫你呼叫model(), 所以在toPost()裡面必須自己將所需要的model產生並當成 參數一併帶在transitionTo()裡面。model()只有當你直接在URL進入post route才會被呼叫。 setupController則是無論使用transitionTo()或是在browser直接打URL進去post route都會被呼叫。

example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
application.PostRoute = Ember.Route.extend({
  setupController: function(controller, model) {
      controller.set('content', model);
  },

  model : function(params){
      return application.Post.find(params.post_id);
  }
});


toPost : function(target_post){
      var model = application.Post.find(target_post);
      this.transitionTo('post', model);
},

nested route使用

一開始以為nest route的命名規則都是parent route name + child route name。 其實不盡然,原來是要看你是使用this.route或是this.resource如果是this.route的話就要加上parent name(如底下的board and summary route)

當要轉換到board這個route就要加上parent route name. (見底下的transitionTo())

example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
MyApp.Router.map(function() {
    this.route("landing", {path: "/landing"});
    this.resource("lobby", {path: "/lobby"}, function(){
      this.route("board", {path: "/board"} );
      this.resource("store", {path: "/store"}, function(){
        this.route("summary", {path: "/summary/:game_id"} );
      });
    });
    this.route("pageNotFound", {path: "*:"});
});

//route naming
MyApp.LandingRoute = Ember.Route.extend({});
MyApp.LobbyRoute = Ember.Route.extend({});
MyApp.LobbyBoardRoute = Ember.Route.extend({});
MyApp.StoreRoute = Ember.Route.extend({});
MyApp.StoreSummaryRoute = Ember.Route.extend({});


toBoard : function(){
  this.transitionTo('lobby.board');
}

Comments