2013-12-13 2 views
5

사용자 지정보기 위치 전략을 사용하는 방법을 파악하려고하는데이 페이지의 설명서를 읽었습니다. http://durandaljs.com/documentation/Using-Composition/ 그러나 전략 기능이 어떤 모양인지 정확히 이해하지 못합니다.Durandal 사용자 지정보기 위치 전략

누구든지이 함수의 구현이 무엇이고 반환하는 약속 (심지어 단순한 것) 등의 간단한 예제를 제공 할 수 있습니까? 사전에

감사합니다, 게리

추신 속성을 읽을 수 없습니다 '디스플레이'이 내 HTML의 코드입니다 :

<div> 
    <div data-bind="compose: {model: 'viewmodels/childRouter/first/simpleModel', strategy: 
'viewmodels/childRouter/first/myCustomViewStrategy'}"></div> </div> 

을이 내 myCustomViewStrategy의 코드입니다 :

define(function() { 

    var myCustomViewStrategy = function() { 

     var deferred = $.Deferred(); 

     deferred.done(function() { console.log('done'); return 'simpleModelView'; }); 
     deferred.fail(function() { console.log('error'); }); 

     setTimeout(function() { deferred.resolve('done'); }, 5000); 

     return deferred.promise(); 
    }; 

return myCustomViewStrategy; 
}); 

하지만 오류 얻을 :

catch되지 않은 형식 오류를 정의되지 않음 - 완료 후 콘솔 창에 기록됩니다.

+0

추신 나는 누군가가 올바른 방향으로 나를 가리킬 수 있다면 나는 감사 할 것입니다. –

답변

1

좋아 나는 다음으로 내 사용자 정의보기 전략을 만들어이 문제를 해결 :

define(['durandal/system', 'durandal/viewEngine'], function (system, viewEngine) { 

    var myCustomViewStrategy = function() { 
     return viewEngine.createView('views/childRouter/first/sModelView'); 
    } 

    return myCustomViewStrategy; 

}); 
1

내가 어떻게 작동하는지 소스 코드를 확인 조금 작성의 결합 전략 설정에 부족한 문서를 발견. 그것을 써머하려면

[작성이의 moduleID에 의해 전략 설정의 결합에 의해 지정된 모듈을

  • '전략'을 준수 할 것을보기에 결과 약속을 반환
  • 라는 함수를 반환해야합니다
  • 을 HTML 요소 개체로 사용합니다.
  • 매개 변수로 전략 메서드는 작성한 바인딩 개체의 설정 개체 인
  • 을 이미받습니다.

동작하는 예제 :

define(['durandal/system', 'durandal/viewEngine'], function (system, viewEngine) { 

    var strategy = function(settings){ 
     var viewid = null; 
     if(settings.model){ 
      // replaces model's module id's last segment ('/viewmodel') with '/view' 
      viewid = settings.model.__moduleId__.replace(/\/[^\/]*$/, '/view'); 
     } 
     return viewEngine.createView(viewid); 
    }; 

    return strategy; 
}); 

Durandal의 소스 :

// composition.js:485 

for (var attrName in settings) { 
    if (ko.utils.arrayIndexOf(bindableSettings, attrName) != -1) { 
     /* 
     * strategy is unwrapped 
     */ 
     settings[attrName] = ko.utils.unwrapObservable(settings[attrName]); 
    } else { 
     settings[attrName] = settings[attrName]; 
    } 
} 

// composition.js:523 

if (system.isString(context.strategy)) { 
    /* 
    * strategy is loaded 
    */ 
    system.acquire(context.strategy).then(function (strategy) { 
     context.strategy = strategy; 
     composition.executeStrategy(context); 
    }).fail(function(err){ 
     system.error('Failed to load view strategy (' + context.strategy + '). Details: ' + err.message); 
    }); 
} else { 
    this.executeStrategy(context); 
} 

// composition.js:501 

executeStrategy: function (context) { 
    /* 
    * strategy is executed 
    * expected to be a promise 
    * which returns the view to be bound and inserted to the DOM 
    */ 
    context.strategy(context).then(function (child) { 
     composition.bindAndShow(child, context); 
    }); 
}