【问题标题】:Use model attribute value in differnt views在不同视图中使用模型属性值
【发布时间】:2013-11-09 11:42:17
【问题描述】:

我是主干的新手,并试图弄清楚如何通过我的单页应用程序传递模型属性。 我正在使用带有 jquery、requirejs 和下划线的主干。

我的代码看起来像 型号:

    define([
    'jquery',
    'underscore',
    'backbone'
], function ($, _, Backbone) {

    'use strict';

    var sessionCall = Backbone.Model.extend({

        url: "http://myserver/sessionCall",

        defaults: {
            APIKey: 'NULL'
        },

        initialize: function() {

            $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
                options.crossDomain ={
                    crossDomain: true
                };
                options.xhrFields = {
                    withCredentials: false
                };
            });
            return this;
        }
    });

    return sessionCall;
    });

登录视图:

 define([
    'jquery',
    'underscore',
    'backbone',
    'router',
    'models/mSessionCall',
    'text!tpl/login.html'
], function ($, _, Backbone, AppRouter model, tpl) {

   // var loginView = Backbone.View.extend({ //if used //return loginView; below

    return Backbone.View.extend({

        events: {
            "click #clb_login":         "callLoginCheck",
            "click #clb_newAccount":    "callNewAccount"
        },

        initialize: function () {

        },

        el: $('#container'),
        render: function () {

            var template = _.template(tpl);
            this.$el.html(template());
        },

        callLoginCheck: function (e) {
            e.preventDefault(); //Don't let this button submit the form

            var loginCheck = new model();
            //Save working version
            loginCheck.save( {
                success: function (loginCheck, data) {
                    loginCheck.set({USR_APIKey: data[0]['USR_APIKey']});
                    appRouter.navigate('home', true);
                },
                error: function(loginCheck, response) {
                    console.log('error');
                    appRouter.navigate('', true);
                }
            });
        },

        callNewAccount: function () {
            appRouter.navigate('newAccount', true);
        }
    });
});

主页视图:

最重要的是工作完美。但是如何在不同的视图中使用我的 USR_APIKey,因为我需要这个密钥才能在我的 REST 服务器上更新/获取/删除我的日期。

有什么想法吗? 谢谢

【问题讨论】:

    标签: jquery backbone.js requirejs


    【解决方案1】:

    我认为最好的方法是将您的 api 密钥保存在某些地方(会话或其他),因为如果您想获取此值,您必须每次都创建一个登录视图并以这种方式获取此属性,例如:

    this.loginView = new LoginView();
    this.loginView.model.get('APIKey');
    

    【讨论】:

      【解决方案2】:

      从浏览器的角度来看,我们有两个熟悉的东西。

      • 会话存储
      • 本地存储

      您可以使用上述任何一种方法来存储 USR_APIKey 的值。

      否则,每次需要 USR_APIKey 值时都必须调用 LoginView(),这不是一个好的做法(就性能而言)。为了摆脱僵尸视图,我们在 UI 范围结束时关闭每个视图。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2017-07-10
        • 2014-08-24
        • 1970-01-01
        • 2014-05-14
        • 1970-01-01
        • 2022-10-19
        • 2014-05-08
        • 1970-01-01
        • 2018-06-15
        相关资源
        最近更新 更多