在 Backbone 应用程序中,按惯例模型、视图和集合的工作原理是什么
我现在正在使用backbonejs mvc javascript 库开发一个非常简单的应用程序。只是为了展示它是多么简单,这里是html
示例Html
<div class="container">
<div>
<label>
Name:
<input id="txtName" type="text" title="Type your name please" /></label>
<button id="btnSubmit" type="button" value="Submit">
Submit</button>
</div>
<div id="names">
</div>
</div>
操作
这是我希望应用程序执行的所有操作。
用户键入名称(或某些字母数字字符串)并单击“提交”。
验证后的值(我认为是他们所说的模型)将被发送到restful服务。
服务将返回相同的字符串以及数据库保存操作的状态。
我现在很困惑点击事件将在哪里处理(是在模型中吗?),之后应该在哪里调用渲染方法?(是在视图中)。下面你会发现我到目前为止管理的脚本
Model.js
//store a global reference to model
window["model"] = Backbone.Model.extend({
defaults: { name: "Type your name"
},
validate: function () {
}
});
View.js
//store a global reference to view
window["view"] = Backbone.View.extend({});
视图中没有任何内容:(
Application.js
//when every thing is ready kick of the application
$(document).ready(function () {
var modelInstance = new model();
});
所以我将单击事件添加到 application.js 或 model.js 中的按钮。这是约定/最佳实践?
我可以渲染吗?从服务返回的名称集合到
#names
中并且数据库插入到容器上方的另一个div
的状态?视图可以渲染两个视图吗?
I am right now developing a dead simple application using backbonejs mvc javascript library.Just to show how simple it is here is the html
Sample Html
<div class="container">
<div>
<label>
Name:
<input id="txtName" type="text" title="Type your name please" /></label>
<button id="btnSubmit" type="button" value="Submit">
Submit</button>
</div>
<div id="names">
</div>
</div>
Operation
Here's all i want the application to do.
User Types a name(or some alphanumeric string) and clicks submit.
The value(which is what they call model i suppose) after validation will be sent to restful service.
Service will return the same string along with status of database save operation.
I am now confused as to where the click event will handled(is it in the model?), after that where should the render method be called?(is it in views). Below you will find the script i had managed up until now
Model.js
//store a global reference to model
window["model"] = Backbone.Model.extend({
defaults: { name: "Type your name"
},
validate: function () {
}
});
View.js
//store a global reference to view
window["view"] = Backbone.View.extend({});
nothing in the view to say :(
Application.js
//when every thing is ready kick of the application
$(document).ready(function () {
var modelInstance = new model();
});
so do i add click event to the button in application.js or model.js.which is the convention/best practice?
Will it be possible for me to render the names collection returned from service into
#names
and status of the database insertion into anotherdiv
somewhere up above the container? Can the view render both views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,这里有一个免责声明:如果这就是您的应用程序所做的全部事情,那么 Backbone 就太仪式化了。当应用程序变得更加复杂并包含更多移动部件时,Backbone 就会变得非常惊人。
话虽这么说,我创建了一个 jsFiddle 来突出显示如何使用 Backbone 来完成您想做的事情:http://jsfiddle。 net/BrianGenisio/CZwnk/4/
基本上,您有一个
Person
模型和一个People
集合来保存Person
模型。然后,您将拥有三个视图:NewPersonView
,这是用于输入数据的表单,负责向People
集合添加新项目。然后,您有一个PeopleView
视图,负责显示People
集合,它将监视该集合并显示所有添加内容。最后是PersonView
,它负责显示单独的Person
模型。当所有这些结合在一起时,它看起来有点像这样:
HTML:
JavaScript
So, here is a disclaimer: If this is all your app is doing, then Backbone is way too much ceremony. When the app gets more complex with more moving parts, Backbone becomes pretty amazing.
That being said, I have created a jsFiddle to highlight how you might use Backbone for what you want to do: http://jsfiddle.net/BrianGenisio/CZwnk/4/
Basically, you have a
Person
model and aPeople
collection to hold thePerson
models. You then have three views:NewPersonView
which is the form for entering your data, which is responsible for adding new items to thePeople
collection. Then you have aPeopleView
view which is responsible for showingPeople
collection, which will watch the collection and show any additions. Finally, is thePersonView
which is responsible for showing an individualPerson
model.When it all comes together, it looks a little something like this:
HTML:
JavaScript
我会在视图中单击
在我看来,你需要 2 个视图,1 个是保存整个应用程序的应用程序视图
另一个只是您在列表中呈现的每个用户模型的视图。
这是你的用户模型,
这是你的集合
,所以这是一个可能的用户视图
,这是你的appView,
你可以在此处初始化所有内容
,如你所见,将其保存在数据库中后不会渲染项目本身,而是将其添加到集合中,appView 绑定到集合的 add 事件并将新用户附加到您的列表中。
由于在集合上使用了创建函数,CRUD 服务器连接会自动工作。
这里的想法是,您不需要使用主干事件,从集合成功保存或错误保存中触发事件,并让任何视图绑定到该事件以使屏幕上发生某些事情保存是否成功。
i would do the click in the view
you would need 2 views in my eyes, 1 is an appview that holds your entire app
the other is just a view for each usermodel you render out in your list .
here is your user model
here is your collection
so here is a possible user view
here is your appView
here you initialize it all
as you can see, rendering the items itself does not happen after saving it in the database, it is added to the collection, the appView binds to the add event of the collection and appends the new user to your list.
the CRUD server connection works automatically, due to using the create function on the collection.
the idea here is that you don't need to user backbone events, trigger an event from your collections successful save or error save, and let any view bind to that event to make something happen on the screen when save was a success or not.