我是一个新的主干,并试图找出如何在我的单页应用程序上传递模型属性。 我正在使用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服务器上更新/获取/删除我的日期。
任何想法? thx