標籤:style blog io 使用 ar java for strong sp
如下代碼會根據model產生不同的table項,在進行其他設定後,一般是根據id來跳轉到相應項目子項中;
{{#each}} {{#link-to "tabls" this}}{{name}}{{/link-to}}{{/each}}
這時候我們可能需要一額外的按鈕而不是直接點擊串連進行子項目的前進與後退,可採用如下方法:
方法1:
在對應controller內設定:
nextId : function(){ var arrs = //獲得對應子項目的content; var thisId = //獲得當前項目的id; for(var idx=0; idx < arrs.length; idx++) { if(arrs[idx].id > thisId) break; } idx = idx < arr.length ? idx : 0; return arrs[idx].id;} prevId : function(){ var arrs = ..; var thisId = ..; for(var idx = arrs.length-1; idx >=0; idx--) { if(arrs[idx].id < thisId) break; } idx = idx >=0 ? : arrs.length - 1; return arrs[idx].id;}
然後在相應模版中設定:
{{#link-to "testResult" nextId}}next{{/link-to}} {{#link-to "testResult" prevId }}prevId{{/link-to}} //這裡說明串連是可以通過直接使用id跳轉;
方法2:
在對應controller內設定:
nextId: function() { var quizs = //擷取項目, index = quizs.indexOf(this.get("model")); return quizs.objectAt(index+1); }.property("model.id"), prevId: function() { var quizs = //擷取項目, index = quizs.indexOf(this.get("model")); return quizs.objectAt(index-1); }.property("model.id"),
然後在模版中設定:
{{#if nextId}} {{#link-to "testResult" nextId}}next{{/link-to}}{{/if}}{{if prevId}} {{#link-to "testResult" prevId }}prevId{{/link-to}} {{/if}}//這裡說明串連也可以通過傳入對應子項目跳轉;
方法3:
在對應controller內設定:
advanceQuiz: function(delta) { var index,length,quizs; quizs = //獲得項目; length = quizs.get("length"); index = (quizs.indexOf(this.get("model")) + delta + length) % length; return this.transitionToRoute("testResult",quizs.objectAt(index)); //這裡使用transitionToRoute,注意後面的route要書寫正確; }, actions: { nextId: function() { return this.advanceQuiz(-1); }, prevId: function() { return this.advanceQuiz(1); } },
然後在模版中直接設定action就可以了,這裡並沒有使用連結,而是使用transitionToRoute方法來跳轉;
ember.js:使用筆記6 子項目的前進與後退