2013-08-29 37 views
0

我正在將我的durandal項目從1.2.0升級到2.0.0版。我已經按照durandal文檔(http://durandaljs.com/documentation/Conversion-Guide/)中的步驟操作,現在應用程序照常運行。我看到的問題是,我的激活回調不斷得到一遍又一遍的調用。2.0.0升級後Durandal ViewModels反覆激活

這裏是做它的ViewModels之一:

define(['services/datacontext', 'plugins/router', 'services/logger', 'services/model', 'services/images', 'services/pager'], 
    function (datacontext, router, logger, model, images, pager) { 

     var collaborators = ko.observableArray([]); 
     var filterString = ko.observable(); 
     var pageHandler = new pager(); 

     var activate = function (filter) { 
      return Q.all([datacontext.getAll(model.entityNames.songwriter + 's', "", collaborators, { orderBy: "firstName" })]) 
        .then(dataRetrieved) 
        .then(activatePager); 

      function dataRetrieved() { 
      }; 

      function activatePager() { 
       pageHandler.init.call(pageHandler, collaborators(), { 
        pageSize: 12, 
        filterCallback: function (item) { 
         var filter = filterString().toLowerCase(); 

         var pred1 = item.fullName().toLowerCase().indexOf(filter) >= 0; 

         return pred1; 
        } 
       }); 
       if (filter != null) { 
        filterString(filter); 
        pageHandler.applyFilter.call(pageHandler); 
       } 
      }; 
     }; 

     var deactivate = function() { 
     }; 

     var vm = { 
      activate: activate, 
      deactivate: deactivate, 
      collaborators: pageHandler.displayItems, 
      title: 'Collaborators', 
      images: images, 
      router: router, 
      pager: pageHandler, 
      filterString: filterString 
     }; 

     return vm; 
    }); 

有沒有人見過這個?

這裏是鍍鉻的控制檯輸出:

Chrome Console

編輯
這似乎並沒有在所有的ViewModels發生的事情,這是一個很好..

define(['durandal/app', 'services/datacontext', 'plugins/router', 'services/logger', 'services/model', 'viewmodels/shared/leftnav', 'viewmodels/modals/imagecrop', 'services/images'], 
    function (app, datacontext, router, logger, model, leftnav, imagecrop, images) { 
     leftnav.area("Songwriter"); 

     var songwriter = ko.observable(); 
     var songwriterId = ko.observable(); 
     var publishers = ko.observableArray([]); 
     var pros = ko.observableArray([]); 

     var activate = function (id) { 
      songwriterId(id); 
      leftnav.entityId(songwriterId()); 

      return Q.all([datacontext.getEntityById(model.entityNames.songwriter, songwriterId(), "Publisher, Pro", songwriter), 
          datacontext.getAll(model.entityNames.publisher + 's', "", publishers), 
          datacontext.getAll(model.entityNames.pro + 's', "", pros)]); 
     }; 

     var deactivate = function() { 
      if (datacontext.hasChanges()) { 
       datacontext.cancelChanges(); 
      } 
     }; 

     var saveClick = function() { 
      datacontext.saveChanges().then(saveComplete); 

      function saveComplete() { 
       router.navigateBack(); 
      }; 
     }; 

     var cancelClick = function() { 
      router.navigateBack(); 
     }; 

     var imageUploaded = function (e) { 
      logger.logSuccess("Image Uploaded Successfully", e.response, "Profile", true); 
      songwriter().photoFilePath('/azure/profileimages/' + e.response); 

      app.showDialog(imagecrop, { 
       title: 'Crop Profile Image', 
       message: '<i class="icon-info-sign"></i> Your profile image needs to be cropped to ensure that it does not appear distorted.', 
       filePath: songwriter().photoFilePath() 
      }).then(appendCropInfo); 

      function appendCropInfo(coords) { 
       var path = songwriter().photoFilePath; 

       path(path() + '?crop=(' + coords.x + ',' + coords.y + ',' + coords.x2 + ',' + coords.y2 + ')'); 
      }; 
     }; 


     var vm = { 
      activate: activate, 
      deactivate: deactivate, 
      songwriter: songwriter, 
      publishers: publishers, 
      pros: pros, 
      title: 'Songwriter', 
      cancelClick: cancelClick, 
      saveClick: saveClick, 
      imageUploaded: imageUploaded, 
      images: images 
     }; 

     return vm; 
    }); 

編輯 - 堆棧跟蹤

activate (details.js:11) 
invoke (activator.js:52) 
activate (activator.js:99) 
(anonymous function) (activator.js:308) 
(anonymous function) (jquery-2.0.3.js:3070) 
fire (jquery-2.0.3.js:2914) 
self.add (jquery-2.0.3.js:2960) 
(anonymous function) (jquery-2.0.3.js:3069) 
jQuery.extend.each (jquery-2.0.3.js:590) 
(anonymous function) (jquery-2.0.3.js:3065) 
jQuery.extend.Deferred (jquery-2.0.3.js:3126) 
promise.then (jquery-2.0.3.js:3064) 
(anonymous function) (activator.js:306) 
(anonymous function) (jquery-2.0.3.js:3070) 
fire (jquery-2.0.3.js:2914) 
self.add (jquery-2.0.3.js:2960) 
(anonymous function) (jquery-2.0.3.js:3069) 
jQuery.extend.each (jquery-2.0.3.js:590) 
(anonymous function) (jquery-2.0.3.js:3065) 
jQuery.extend.Deferred (jquery-2.0.3.js:3126) 
promise.then (jquery-2.0.3.js:3064) 
(anonymous function) (activator.js:302) 
(anonymous function) (jquery-2.0.3.js:3070) 
fire (jquery-2.0.3.js:2914) 
self.add (jquery-2.0.3.js:2960) 
(anonymous function) (jquery-2.0.3.js:3069) 
jQuery.extend.each (jquery-2.0.3.js:590) 
(anonymous function) (jquery-2.0.3.js:3065) 
jQuery.extend.Deferred (jquery-2.0.3.js:3126) 
promise.then (jquery-2.0.3.js:3064) 
(anonymous function) (activator.js:300) 
jQuery.extend.Deferred (jquery-2.0.3.js:3126) 
system.defer (system.js:218) 
computed.activateItem (activator.js:285) 
activateRoute (router.js:248) 
handleGuardedRoute (router.js:303) 
ensureActivation (router.js:313) 
(anonymous function) (router.js:357) 
(anonymous function) (jquery-2.0.3.js:3070) 
fire (jquery-2.0.3.js:2914) 
self.fireWith (jquery-2.0.3.js:3026) 
deferred.(anonymous function) (jquery-2.0.3.js:3115) 
(anonymous function) (system.js:256) 

編輯 - 路線

的路由配置如下:

{ route: 'songwriter/:id', moduleId: 'songwriter/details', nav: true, title: 'Profile', settings: { icon: 'icon-edit' } }, 
+0

如何第一視圖模型被組成?它是一個局部視圖,模態對話框等嗎? – Brett

+0

第一個視圖模型是全視圖。它的組成與第二個相同。 – mcottingham

+0

很難說。有很多代碼缺失,我只能猜測它在做什麼。在'activate()'函數中添加'debugger;'作爲第一行,然後遍歷代碼,找出觸發它的原因。 – Brett

回答

0

可以是我升級後觀測到2.0同樣的問題。 我在谷歌迪朗達爾發佈組類似的案例:https://groups.google.com/forum/m/?fromgroups#!topic/durandaljs/rOQ8EfFb1tM

我創建了一個回購來重現問題:https://github.com/stiankroknes/durandal-router-problem

+0

是的,這看起來是同樣的問題。解決方案有任何進展嗎? – mcottingham

+0

是的,這是Durandal 2中的一個錯誤。請參閱最近的評論:https://groups.google.com/forum/#!topic/durandaljs/rOQ8EfFb1tM –