ember.js:使用筆記6 子項目的前進與後退

來源:互聯網
上載者:User

標籤: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 子項目的前進與後退

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.