1

我正在尝试将firebase登录机制集成到我基于backbone.js和require.js的phonegap应用程序中。我成功设置了使用 facebook 登录并使用 twitter 登录,它们似乎工作正常。

现在我对最后一个有问题:使用电子邮件和密码登录......我不知道发生了什么,但似乎使用密码登录并没有调用我的 authclient 定义中的回调方法。这是我在执行开始时用 require 调用的主文件:

  require.config({
    paths: {
    domReady: '../lib/require/domReady',
    text: '../lib/require/text-1.0.6',
    async: '../lib/require/async',
    zepto: '../lib/zepto/zepto',
    underscore: '../lib/underscore/underscore-min',
    backbone: '../lib/backbone/backbone',
    handlebars: '../lib/handlebars/handlebars',
    firebase: '../lib/firebase/firebase',
    backfire: '../lib/firebase/backfire',
    fireauth: '../lib/firebase/firebase-auth-client',
    leaflet: '../lib/leaflet/leaflet',
    barcodescanner: '../lib/barcodescanner/barcodescanner',
    templates: '../templates',
  },
  shim: {
    'zepto': {
      exports: '$'
    },
    'underscore': {
    exports: '_'
    },
    'backbone': {
        deps: ['zepto', 'underscore'],
        exports: 'Backbone'
    },
    'handlebars': {
        exports: 'Handlebars'
    },
    'firebase': {
        exports: 'Firebase'
    },
    'backfire': {
        deps: ['backbone','firebase'],
        exports: 'Backfire'
    },
    'fireauth': {
      deps: ['firebase'],
      exports: 'Fireauth'
    },
    'leaflet': {
        exports: 'L'
    },
    'barcodescanner': {
        exports: 'Barcodescanner'
    }
  }
});


require(['zepto','domReady','underscore','backbone','firebase','fireauth','router'],
    function ($,domReady, _,Backbone,Firebase,Fireauth,AppRouter) {

    domReady(function () {
      document.addEventListener("deviceready", run, false);
    });

    function run() {

        firebaseRef = new Firebase('https://cicero.firebaseio.com');
        authClient = new FirebaseAuthClient(firebaseRef, function(error, user) {
            if (error) {
                alert("error during user login");
            } else if (user) {
                    auth = user;
                    Backbone.history.navigate("map", {trigger: true});

                    } else {
                            auth = undefined;
                    }
          });

        new AppRouter();
        Backbone.history.start();
    }
  });

这是我调用登录方法的视图:

define(["zepto", "underscore", "backbone", "handlebars","firebase","fireauth","text!templates/loginView.html"],
    function ($, _, Backbone, Handlebars,Firebase,Fireauth,template) {

    var loginView = Backbone.View.extend({

        events: {
            "touchstart #login" : "login",
            "touchstart #register" : "showRegistration",
            "touchstart #guest" : "showMap",
            "touchstart #facebook" : "loginFacebook",
            "touchstart #twitter" : "loginTwitter"
          },

        template: Handlebars.compile(template),

        initialize: function () {
            this.render();
        },

        render: function (eventName) {
            $(this.el).empty();
            $(this.el).html(this.template());
            return this;
        },

        showRegistration: function () {
            Backbone.history.navigate("register", {trigger: true});
        },

        showMap: function () {
            Backbone.history.navigate("map", {trigger: true});
        },

        login: function(){
            var user_email = $('#email').attr('value');
            var user_password = $('#password').attr('value');
            authClient.login("password", {
                email: user_email,
                password: user_password
              });
        },

        loginFacebook: function(){
            authClient.login("facebook");
        },

        loginTwitter: function(){
            authClient.login("twitter");
        }

      });

    return loginView;

  });

如您所见,我使用 3 个全局变量(firebaseRef、authCLient 和 auth)来在我的应用程序的每个部分中获取它们的引用,即使我不知道这是否是一个好方法。如果我尝试使用 facebook 和 twitter 登录,它可以工作并且回调函数将我重定向到新页面内,使用密码和电子邮件登录,而不是刷新页面,但它不会改变它。

4

0 回答 0