2013-01-04 2 views
0

Simple Login implementation for Dojo MVC /을 참조하면 이해할 수없는 한 가지 점이 있습니다. phusick의 샘플과 관련하여 로그인 대화 상자 클래스는 dom.byId ("dialog-template") 호출을 수행합니다. - "dialog-template"은 대화 상자의 템플릿 인 스크립트의 ID입니다. html 템플릿 - 메인 HTML에는 없습니다. main.js (포함 - 나는 것을 제거한다면, dom.byId에 대한 호출이Dojo 위젯 템플릿

가 이

main.html을 (아무것도에만 호출하지 main.js 호출)는 다음과 같이

그래서 내 코드의 구조는 실패

require([ 
"dojo/_base/declare","dojo/_base/lang","dojo/on","dojo/dom","dojo/Evented", 
"dojo/_base/Deferred","dojo/json","dijit/_Widget","dijit/_TemplatedMixin", 
"dijit/_WidgetsInTemplateMixin","dijit/Dialog", 
"widgets/LoginDialog", 
"widgets/LoginController", 
"dijit/form/Form","dijit/form/ValidationTextBox","dijit/form/Button", 

"dojo/domReady!" 
], function(
declare,lang,on,dom,Evented,Deferred,JSON, 
_Widget, 
_TemplatedMixin, 
_WidgetsInTemplateMixin, 

Dialog, 
LoginDialog, 
LoginController 
) { 
// provide username & password in constructor 
// since we do not have web service here to authenticate against  
var loginController = new LoginController({username: "user", password: "user"}); 

var loginDialog = new LoginDialog({ controller: loginController}); 
loginDialog.startup(); 
loginDialog.show(); 

loginDialog.on("cancel", function() { 
    console.log("Login cancelled.");   
}); 

loginDialog.on("error", function() { 
    console.log("Login error."); 
}); 

loginDialog.on("success", function() { 
    console.log("Login success."); 
    console.log(JSON.stringify(this.form.get("value"))); 
}); 


}); 

지금 LoginDialog.js 및 LoginDialogTemplate.html 대화 에 대한 templatised 위젯과 LoginController.js는 컨트롤러) 다음과 같습니다.

내 LoginDialog.js 템플릿 그래서 난 위젯이를 호출 할 때 추측 ID = "대화 템플릿"을 가지고 있기 때문에

<script type="text/template" id="dialog-template"> 
<div style="width:300px;"> 

    <div class="dijitDialogPaneContentArea"> 
     <div data-dojo-attach-point="contentNode"> 
      {form}    
     </div> 
    </div> 

    <div class="dijitDialogPaneActionBar"> 
     <div 
      class="message" 
      data-dojo-attach-point="messageNode"     
     ></div>  
     <button 
      data-dojo-type="dijit.form.Button" 
      data-dojo-props=""     
      data-dojo-attach-point="submitButton" 
     > 
      OK 
     </button> 

     <button 
      data-dojo-type="dijit.form.Button" 
      data-dojo-attach-point="cancelButton" 
     > 
      Cancel 
     </button> 
    </div>  
</div> 
</script> 

을 다음과 같이

define([ 
"dojo/_base/declare","dojo/_base/lang","dojo/on","dojo/dom","dojo/Evented","dojo/_base/Deferred","dojo/json", 

"dijit/_Widget","dijit/_TemplatedMixin","dijit/_WidgetsInTemplateMixin", 
"dijit/Dialog","dijit/form/Form","dijit/form/ValidationTextBox","dijit/form/Button", 
"dojo/text!templates/loginDialogTemplate.html", 
"dojo/text!templates/loginFormTemplate.html", 

"dojo/domReady!" 
], function(
declare,lang,on,dom,Evented,Deferred,JSON, 
_Widget, 
_TemplatedMixin, 
_WidgetsInTemplateMixin, 
Dialog, 
Form, 
Button, 
template 

) { 

return declare([ Dialog, Evented], { 

    READY: 0, 
    BUSY: 1, 

    title: "Login Dialog", 
    message: "", 
    busyLabel: "Working...", 

    // Binding property values to DOM nodes in templates 
    // see: http://www.enterprisedojo.com/2010/10/02/lessons-in-widgetry-binding-property-values-to-dom-nodes-in-templates/ 
    attributeMap: lang.delegate(dijit._Widget.prototype.attributeMap, { 
     message: { 
      node: "messageNode", 
      type: "innerHTML"    
     }    
    }), 

    constructor: function(/*Object*/ kwArgs) { 
     lang.mixin(this, kwArgs);    
     var dialogTemplate = dom.byId("dialog-template").textContent; 
     var formTemplate = dom.byId("login-form-template").textContent; 
     var template = lang.replace(dialogTemplate, { 
      form: formTemplate     
     }); 

     var contentWidget = new (declare(
      [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], 
      { 
       templateString: template     
      } 
     )); 
     contentWidget.startup(); 
     var content = this.content = contentWidget; 
     this.form = content.form; 
     // shortcuts 
     this.submitButton = content.submitButton; 
     this.cancelButton = content.cancelButton; 
     this.messageNode = content.messageNode; 
    }, 

    postCreate: function() { 
     this.inherited(arguments); 

     this.readyState= this.READY; 
     this.okLabel = this.submitButton.get("label"); 

     this.connect(this.submitButton, "onClick", "onSubmit"); 
     this.connect(this.cancelButton, "onClick", "onCancel"); 

     this.watch("readyState", lang.hitch(this, "_onReadyStateChange")); 

     this.form.watch("state", lang.hitch(this, "_onValidStateChange")); 
     this._onValidStateChange(); 
    }, 

    onSubmit: function() { 
     this.set("readyState", this.BUSY); 
     this.set("message", ""); 
     var data = this.form.get("value"); 

     var auth = this.controller.login(data); 

     Deferred.when(auth, lang.hitch(this, function(loginSuccess) { 
      if (loginSuccess === true) { 
       this.onLoginSuccess(); 
       return;      
      } 
      this.onLoginError(); 
     })); 
    }, 

    onLoginSuccess: function() { 
     this.set("readyState", this.READY); 
     this.set("message", "Login sucessful.");    
     this.emit("success"); 
    }, 

    onLoginError: function() { 
     this.set("readyState", this.READY); 
     this.set("message", "Please try again."); 
     this.emit("error");   
    }, 

    onCancel: function() { 
     this.emit("cancel");  
    }, 

    _onValidStateChange: function() { 
     this.submitButton.set("disabled", !!this.form.get("state").length); 
    }, 

    _onReadyStateChange: function() { 
     var isBusy = this.get("readyState") == this.BUSY; 
     this.submitButton.set("label", isBusy ? this.busyLabel : this.okLabel); 
     this.submitButton.set("disabled", isBusy); 
    }    
}); 
}); 

내 loginDialogTemplate.html이입니다 dom.byId ("dialog-template"), 줄에 "TypeError : dom.byId (...) is null"이라는 오류가 발생합니다. -> var dialogTemplate = dom.byId ("dialog-template"). ;

여기서 내가 뭘 잘못하고 있니?

기본 HTML에서 모든 템플릿 스크립트를 사용하면 정상적으로 작동합니다.

답변

0

아시프,

당신이 정의 함수에서 템플릿을 전달하고 있기 때문에,이 내용을 얻을 수있는 dom.byId()를 필요로하지 않습니다. 이것을 시도하십시오 :

HTML 템플리트에서 요소를 제거하십시오. 당신은 다음 변경 내용 formTemplate이 필요합니다

... 
Button, 
dialogTemplate, 
formTemplate 

: LoginDialog.js에서

은, 당신의 함수 인수를 변경합니다. 'templateTemplate'대신 'dialogTemplate'을 사용 했으므로 예제의 코드를 대체하는 방법이 더 분명합니다. 그런 다음 생성자의 시작 부분을 다음과 같이 변경하십시오.

변경된 내용을 볼 수 있도록 주석이있는 코드 만 남았습니다. 그것이하는 일은 dialogTemplate HTML의 {form} 자리 표시자를 전달한 formTemplate으로 대체하여 'template'이라는 새 템플릿 문자열을 만드는 것입니다. 그런 다음 새 템플릿 문자열을 사용하여 위젯을 만듭니다.

+0

감사합니다. 아직 시도하지 않았습니다 - GWT로 이동하는 것을 고려해야합니다 (Vaadin을 드라이브로 보냈지 만) GWT가 내 개발 필요성에 맞게 보입니다. –