0

ログインという非常に単純なものを作成しようとしています。

これは私のテンプレートです:

        <script type="text/x-handlebars" data-template-name="login">
        <div class="row well">
            <form class="form-inline">
                {{input value=email type="text" placeholder="Email?"}}
                {{input value=password type="password" placeholder="Password?"}}

                {{#view App.LoginView}}
                    <button id="login-button" type="submit" class="btn">Sign in</button>
                {{/view}}
            </form>

            {{#if not_logged}}
                <div class="alert alert-error">
                  <button type="button" class="close" data-dismiss="alert">&times;</button>
                  <strong>Warning!</strong> Best check yo self, you're not looking too good.
                </div>
            {{/if}}
        </div>
    </script>

景色:

App.LoginView = Ember.View.extend(
{
    tagName: "span",

    click: function()
    {
        this.$("#login-button").button("loading");
        this.get("controller").send("do_login");
    }
});

コントローラ:

App.LoginController = Ember.Controller.extend(
{
    is_logged: true,

    do_login: function()
    {
        /** some ajax comes here **/
    }
});

したがって、ユーザーがボタンをクリックすると、状態が「読み込み中」に変わります。問題は、ログインが失敗した場合、どうすればそれをビューに通知して、これを実行できるかということです. $("#login-button").button("reset"); ?

4

2 に答える 2

1

所有者コンポーネントでブーストラップ ボタンをラップし、バインディングを使用してこれを行いました。jquery を使用してボタンを検索する代わりに、これが残り火の方法だと思います。

コントローラー:

App.LoginController = Ember.ObjectController.extend({
    not_logged: false,
    email: 'foo@bar.com',
    password: 'ember',
    isLoading: false,
    doLogin: function() {
        this.set('isLoading', true);
        // simulates some ajax
        Ember.run.later(this, function() {
            if (this.get('email') == 'foo@bar.com' && this.get('password') == 'ember') {
                this.set('not_logged', false);
                // transition to other route etc
            } else {
                this.set('not_logged', true);
            }
            this.set('isLoading', false);
        }, 1000);
    }
});

カスタム ボタン コンポーネント:

App.BoostrapButton = Ember.View.extend({ 
    tagName: 'button',
    attributeBinding: ['type'],
    type: 'submit',
    classNames: ['btn'],
    isLoading: true,
    isLoadingChanged: function() {        
        debugger;
        if (this.get('isLoading')) {            
            this.$().button('loading');
        } else {
            this.$().button('reset');
        }
    }.observes('isLoading')    
})

このjsfiddle

このjsfiddleを見てください

ここでのボーナスは、ログインの仕方を示す embercasts のエピソードです。

于 2013-08-06T19:40:37.060 に答える