簡體   English   中英

Ember.js-我希望在過渡到新路線時觸發Action事件(on =“”)

[英]Ember.js - I want an Action event (on=“”) to trigger when there is a transition to a new Route

我希望在過渡到新路線時觸發Action事件(on =“”)。

我已經看到了動作事件處理程序的列表,而我能找到的最接近的動作是將動作附加到頁面上最大的HTML元素上,並使用“ Mousemove”觸發它。這是我要做的一件非常有缺陷的事情。

所以只是畫出來。

<div {{action 'displayEitherHtml1or2'}} class="largestDOMelement">
  {{#if showHtml1}}
   // html 1 inside
  {{/if}}
  {{#if showHtml2}}
  // html 2 inside
  {{/if}}
</div>

“ / objects”是一個對象列表,單擊一個將導致“ object / somenumber”。 當我進入“ object / somenumber”頁面時,該操作應自動觸發。

更新:我已經從上次更新中獲取了內容並將其轉儲到我的DocRoute中,但是當我通過{{#link-to'doc'this.docID}} {{docTitle} }{{/鏈接到}}

VpcYeoman.DocRoute = Ember.Route.extend(VpcYeoman.Authenticated,{
    toggleLetterSwitch: false,
    togglePermitSwitch: false,
    activate: function () {  
      var docTemplateID = this.get('docTemplateID');
      if ( docTemplateID == 2) {
        this.set('toggleLetterSwitch', true);
        this.set('togglePermitSwitch', false);
        console.log('docTemplateID equals 2');
      } else {
        this.set('toggleLetterSwitch', false);
        this.set('togglePermitSwitch', true);
      }        
    }
});

UPDATE DOS:在DocsController中將setDocID設置為1。這就是全部。

VpcYeoman.DocsController = Ember.ArrayController.extend({ 
  tempDocId: 1, 
  actions: {
    addDoc: function (params) {
      var docTitle = this.get('docTitle');
      var docTemplateID = 1;
      var docTemplateID = this.get('tempDocId');
      console.log(this.get('tempDocId'));
      var store = this.store;
      var current_object = this;
      var doc = current_object.store.createRecord('doc', {
        docTitle:docTitle,
        docTemplateID:docTemplateID
      });
      doc.save();
      return true;
    },
    setDocId: function (param) {
      this.set('tempDocId', param);
      console.log(this.get('tempDocId'))
    },
  }
});

作為@fanta評論說,好像你正在尋找activate路線之內。 當您輸入定義路徑時,將調用此方法。 如果要在每次轉換時都調用它,則可以考慮為應用程序定義一個基本路由,並擴展它而不是Em.Route:

App.BaseRoute = Em.Route.extend(
    activate: function () {
        // Do your thing
    }
);
App.YourRoutes = App.BaseRoute.extend()

可能有一個更合適的位置/時間來執行此操作,但是在不完全了解您的操作的情況下,這可能是最好的猜測。

ETA:查看您的編輯,您不希望所有路由都像我上面那樣擴展App.BaseRoute; 您應該只在需要它的路由中明確包含該activate鈎子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM