Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 什么是Ember.bind,它是如何工作的?_Javascript_Ember.js_Ember Cli - Fatal编程技术网

Javascript 什么是Ember.bind,它是如何工作的?

Javascript 什么是Ember.bind,它是如何工作的?,javascript,ember.js,ember-cli,Javascript,Ember.js,Ember Cli,您好,我已经阅读了和一些,但我仍然不明白下面代码中的.bind(这个)发生了什么 代码对我来说很有用,但我讨厌不理解正在发生的事情 如果我从下面的代码中删除.bind(这个),会发生什么 什么和什么有联系 您正在回调函数上使用.bind(this)。这意味着,函数中的this通常会引用调用函数的上下文(这是JavaScript,与其他语言的上下文不同)。您希望此引用当前上下文。这正是.bind(this)所做的:它确保使用当前上下文(而不是默认上下文)调用回调函数。Ha!这只是我对javascr

您好,我已经阅读了和一些,但我仍然不明白下面代码中的.bind(这个)发生了什么

代码对我来说很有用,但我讨厌不理解正在发生的事情

  • 如果我从下面的代码中删除.bind(这个),会发生什么
  • 什么和什么有联系

  • 您正在回调函数上使用.bind(this)。这意味着,函数中的
    this
    通常会引用调用函数的上下文(这是JavaScript,与其他语言的上下文不同)。您希望
    引用当前上下文。这正是
    .bind(this)
    所做的:它确保使用当前上下文(而不是默认上下文)调用回调函数。

    Ha!这只是我对javascript的基本理解不足,与ember.js无关,难怪我找不到任何文档:)谢谢!这是标准的JS:在旁注中有一个
    Ember.run.bind
    ,用于将第三方侦听器/函数添加到运行循环中。

    import Ember from 'ember';
    
    // Since I've defined my url in environment.js I can do this
    
    import ENV from '../config/environment';
    var ref = new window.Firebase(ENV.firebase);
    
    export default {
        name: 'session',
    
        // Run the initializer after the store is ready
        after: 'store',
    
        initialize: function(container, app) {  
            // session object is nested here as we need access to the container to get the store
            var session = Ember.Object.extend({
    
                // initial state
                authed: false,
    
                init: function() {
                    // get access to the ember data store
                    this.store = container.lookup('store:main');
                    // on init try to login
                    ref.onAuth(function(authData) {
    
                        // Not authenticated
                        if (!authData) {
                            this.set('authed', false);
                            this.set('authData', null);
                            this.set('user', null);
                            return false;
                        }
    
                        // Authenticated
                        this.set('authed', true);
                        this.set('authData', authData);
                        this.afterAuthentication(authData.uid);
                    ***}.bind(this));***
                },
    
                // Call this from your Ember templates
                login: function(provider) {
                    this._loginWithPopup(provider);
                },
    
                // Call this from your Ember templates
                logout: function() {
                    ref.unauth();
                },
    
                // Default login method
                _loginWithPopup: function(provider) {
                    var _this = this;
                    // Ember.debug('logging in with popup');
                    ref.authWithOAuthPopup(provider, function(error, authData) {
                        if (error) {
                            if (error.code === "TRANSPORT_UNAVAILABLE") {
                                // fall-back to browser redirects, and pick up the session
                                // automatically when we come back to the origin page
                                _this._loginWithRedirect(provider);
                            }
                        } else if (authData) {
                            // we're good!
                            // this will automatically call the on ref.onAuth method inside init()
                        }
                    });
                },
    
                // Alternative login with redirect (needed for Chrome on iOS)
                _loginWithRedirect: function(provider) {
                    ref.authWithOAuthRedirect(provider, function(error, authData) {
                        if (error) {
    
                        } else if (authData) {
                            // we're good!
                            // this will automatically call the on ref.onAuth method inside init()
                        }
                    });
                },
    
                // Runs after authentication
                // It either sets a new or already exisiting user
                afterAuthentication: function(userId) {
                    var _this = this;
    
                    // See if the user exists using native Firebase because of EmberFire problem with "id already in use"
                    ref.child('users').child(userId).once('value', function(snapshot) {
                        var exists = (snapshot.val() !== null);
                        userExistsCallback(userId, exists);
                    });
    
                    // Do the right thing depending on whether the user exists
                    function userExistsCallback(userId, exists) {
                        if (exists) {
                            _this.existingUser(userId);
                        } else {
                            _this.createUser(userId);
                        }
                    }
                },
    
                // Existing user
                existingUser: function(userId) {
                    var _this = this;
                    this.store.find('user', userId).then(function(user) {
                        _this.set('user', user);
                    ***}.bind(this));***
                },
    
                // Create a new user
                createUser: function(userId) {
                    var _this = this;
    
                    this.get('store').createRecord('user', {
                        id: userId,
                        provider: this.get('authData.provider'),
                        name: this.get('authData.facebook.displayName') || this.get('authData.google.displayName'),
                        email: this.get('authData.facebook.email') || this.get('authData.google.email'),
                        created: new Date().getTime()
                    }).save().then(function(user){
    
                        // Proceed with the newly create user
                        _this.set('user', user);
                    });
                },
    
                // This is the last step in a successful authentication
                // Set the user (either new or existing)
                afterUser: function(user) { 
                    this.set('user', user);
                }
            });
    
            // Register and inject the 'session' initializer into all controllers and routes
            app.register('session:main', session);
            app.inject('route', 'session', 'session:main');
            app.inject('controller', 'session', 'session:main');
        }
    }; <code>