2014-09-02 4 views
0

비밀번호를 제어하고 javascript의 도움으로 일치하는 비밀번호 필드를 확인하기 위해 ember 컨트롤로 javascript 코드를 작성하고 싶습니다. emberjs와 함께 할 수 있으면 알려주세요.이 코드를 사용해 보았습니다.ember js (비밀번호는 emberjs와 일치하는 비밀번호 확인 비밀번호 확인)

http://jsfiddle.net/8MRcS/12/ 

내가 그 wokring보다 app.js 컨트롤러에 넣고 경우 그것의 가입 컨트롤에서 완벽하게 작동하지 않는 것보다 내 CONTORL에서이 코드를 넣어하지만 난 가입 코드 내에서이 필요로 할 때 나도 몰라.

여기 내 템플릿 코드입니다.

<script type="text/x-handlebars" data-template-name="signup"> 

     <h2>Sign Up</h2> 
     {{outlet}} 

    <form {{action "submit" on="submit" }}> 
     <!-- //This connects to the controller, using the user object --> 


     <p><label>Name: {{input type="text" value=user.name required="true"}}</label></p> 
     <p><label>Username: {{input type="text" value=user.username required="true"}}</label></p> 
     <p><label>Password: {{input value=user.password type="password" id="password" required="true"}}</label></p> 

     <p><label>Confirm Password: {{input value=user.confirm type="password" id="confirm" required="true"}}</label></p> 
     </br> 

     <p><label>Email: {{input type="email" value=user.email required="true"}}</label></p> 
     <p><label>Address: {{input type="text" value=user.address required="true"}}</label></p> 
     <p><label>Age: {{input type="number" value=user.age required="true"}}</label></p> 
     <button id="submit" {{bind-attr disabled="cannot_submit"}}>Submit form</button> 
     <button {{action "clear"}}>Clear</button> 
     <p></p> 
     <button> {{#link-to 'login'}}Already member{{/link-to}}</button> 
    </form> 

    {{message}} 
</script> 

가입 컨트롤러 코드.

App.SignupController = Ember.ObjectController.extend({ 

    // User is the model used for binding the view and the controller 
    // If you look at the view, you will see user.username, and user.password being 
    // used as input values 
    // BTW this is a JavaScript object, (i.e in JSON) 

    user: { 
     //These values are just place holders for the moment 
     name: "", 
     username: "", 
     email: "", 
     address: "", 
     age: "", 
     password: "", 
     confirm: "", 

    }, 



    // The functions called when and event is triggered from the view (i.e button click) 
    actions: { 
     // Action called when form is submitted, refer to form action attribute in view 
     submit: function() { 

      var self = this; 
      var submitUser = {}; 

      submitUser.name = self.user.name; 
      submitUser.username = self.user.username; 
      submitUser.password = self.user.password; 
      submitUser.cpassword = self.user.confirm; 
      submitUser.email = self.user.email; 
      submitUser.address = self.user.address; 
      submitUser.age = self.user.age; 



      // This is the API call to the web services, it uses the user (this.user) to send 
      // the login info, and recives either the logged in user info, or an error 

       console.debug(this.user); 
       console.debug(this.submitUser); 


      $.post("http://herukoapp.com/users", JSON.stringify(this.user), function(data,status){ 
       data = JSON.parse(data); 


       console.debug(data); 
      }); 
     }, 

     // Called when the clear button is clicked, see button tag in view 
     clear: function() { 
      this.set('user.name', ""); 
      this.set('user.username', ""); 
      this.set('user.password', ""); 
      this.set('user.confirm', ""); 
      this.set('user.email', ""); 
      this.set('user.address', ""); 
      this.set('user.age', ""); 


     } 
    }, 


}); 
+0

내가 암호 일치 할 것입니다 귀하의 SignupController에서 아무 것도 볼니까. ApplicationController에 이것을 남겼습니까? – tikotzky

답변

0

computed property을 사용하면 user.password와 user.confirm이 같은지 여부를 추적 할 수 있습니다. 커피 스크립트에서

:

confirmSame: (-> 
    password = @get('user.password') 
    confirm = @get('user.confirm') 

    if password == confirm 
     return true 
    else 
     return false 
).property('user.password', 'user.confirm') 

또는 일반 자바 스크립트

는 (내 생각) :

confirmSame: function() { 
    password = this.get('user.password') 
    confirm = this.get('user.confirm') 
    if password == confirm 
     return true 
    else 
     return false 
}.property('user.password', 'user.confirm') 

당신은 템플릿이있는 조건을 수행 할 수 있습니다

{{#if confirmSame)) 
    <div>Password and Confirm Password are the same</div> 
{{else}} 
    <div>Password and Confirm Password are not the same</div> 
{{/if}} 

그리고에 제출 작업은 confirmSame이 true인지 false인지 확인할 수 있습니다.

커피 스크립트 :

if @get('confirmSame') 
    # do submit stuff 

또는 자바 스크립트

(나는 생각한다) :

if (this.get('confirmSame') == true) 
    // do submit stuff 
+0

여기서 () if (this.get ('confirmSame') == true) ?? –