我开始学习backbone.js并且我已经构建了我的第一页,我想知道如果我走下'正确'的路径(就像软件中有正确的路径一样)。
是否可以让模型属性(属性)自动绑定到html元素?
html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>settings page</title>
<link rel="stylesheet" type="text/css" href="../Content/theme.css" />
<script language="javascript" src="../Scripts/jquery-1.7.1.js"></script>
<script language="javascript" src="../Scripts/underscore.js"></script>
<script language="javascript" src="../Scripts/backbone.js"></script>
<script language="javascript" src="../Scripts/settings.js"></script>
</head>
<body>
<div style="width:95%;margin:10px;padding:10px;background-color:#ffffff;color:#000000;padding-bottom:8px;padding-right:5px;padding-top:4px;float:left;">
<h1>
Settings...
</h1>
Server URL (cloud based API):
<br />
<input id="settings-service-url" type="text" size="100" />
<br />
<br />
Timeout:
<br />
<input id="settings-timeout" type="text" size="100" />
<br />
<br />
<button id="update-settings">Update Settings</button>
</div>
</body>
</html>
使用Javascript:
$(document).ready(function () {
if (typeof console == "undefined") {
window.console = { log: function () { } };
}
Settings = Backbone.Model.extend({
defaults: {
ServiceUrl: "",
Timeout: 0
},
url: function () {
return '/settings';
},
replaceServiceUrlAttr: function (url) {
this.set({ WisdomServiceUrl: url });
},
replaceTimeoutAttr: function (timeout) {
this.set({ Timeout: timeout });
}
});
SettingsView = Backbone.View.extend({
tagName: 'li',
events: {
'click #update-settings': 'updateSettings'
},
initialize: function () {
_.bindAll(this, 'render');
this.settings = new Settings;
this.settings.fetch({ success: function () {
view.render(view.settings);
}
});
},
updateSettings: function () {
view.settings.replaceServiceUrlAttr($('#settings-service-url').val());
view.settings.replaceTimeoutAttr($('#settings-timeout').val());
view.settings.save();
},
render: function (model) {
$('#settings-wisdom-service-url').val(model.get("WisdomServiceUrl"));
$('#settings-timeout').val(model.get("Timeout"));
}
});
var view = new SettingsView({ el: 'body' });
});
答案 0 :(得分:1)
您认为有误。首先,通常的做法是在创建新视图时将模型作为参数传递:
var view = new SettingsView({ "el": "body", "model": new Settings() });
现在,您可以在视图中通过this.model
访问您的模型。
接下来就是在视图中使用变量view
。使用Backbone的View意味着您可以拥有一个View类的多个实例。因此,调用new SettingsView()
会创建一个视图实例。让我们考虑一下你的观点的两个实例:
var view = new SettingsView({ "el": "body", "model": new Settings() });
var view1 = new SettingsView({ "el": "body", "model": new Settings() });
每当你在一个实例中调用view.settings.save();
时,它总是会在第一个视图实例中调用该方法,因为它绑定了变量名“view”。因此,您只需使用this
而不是view
:
SettingsView = Backbone.View.extend({
tagName: 'li',
events: {
'click #update-settings': 'updateSettings'
},
initialize: function () {
this.settings = new Settings;
this.settings.fetch({ success: _.bind(function () {
//to get this work here we have to bind "this",
//otherwise "this" would be the success function itself
this.render(view.settings);
}, this)
});
},
updateSettings: function () {
this.model.replaceServiceUrlAttr($('#settings-service-url').val());
this.model.replaceTimeoutAttr($('#settings-timeout').val());
this.model.save();
},
render: function () {
$('#settings-wisdom-service-url').val(this.model.get("WisdomServiceUrl"));
$('#settings-timeout').val(this.model.get("Timeout"));
}
});
在模型中使用这两种设置方法目前没有多大意义,因为它们只是调用set。所以你可以直接在模型上调用set。
同样使用tagName: 'li'
并插入元素将无法正常工作。如果不在构造函数中插入元素,则仅使用tagName有效。在这种情况下,backbone将使用tagName创建一个新元素。否则,视图的元素是您传递给构造函数的元素。