将我的旧工作流程调整为 Backbone
我开始构建一个新的应用程序,我想使用 Backbone 作为我的框架。以下是该应用程序(以及大多数应用程序)遵循的基本工作流程。
与 Backbone 一起使用的正确/最佳模型是什么?
老方法
用户导航到页面。
选择“创建新小部件”
用户会看到一个充满输入的表单
此时,我可能会获取输入的值(通过基本验证后),将它们包装起来并通过 ajax 请求将它们发送到服务器
请求返回“OK”,用户被带到其他地方(这一步并不完全重要)
一些基本的伪代码
// Grab values
var userName = $('.UserName').val(),
dateOfBirth = $('.DateOfBirth').val();
...
...
...
$.ajax({
url: "/Webservices/ProcessStuff",
success: function(result){
if (result) {
// Render something or doing something else
} else {
// Error message
}
},
error: function () {
// Error message
}
});
Backbone way
使用与上面相同的示例;我假设我有一个用户信息模型和一个显示输入的视图。然而,处理对 Web 服务的实际调用是我感到困惑的事情之一。这需要去哪里?在模型或视图中单击
某个“Go”按钮?
Model.UserInformation = Backbone.Model.extend({ username: null, dateOfBirth: null });
也许还有这些 UserInformation 模型的集合?UserInformations = Backbone.Collection.extend({ model: Model.UserInformation' });
所以我要问的底线是......
实现此功能的最佳方法是什么?
实际执行 CRUD 的正确方法是什么?在哪里放置对删除/更新/创建/等的实际调用?
Im starting to build a new app and I would like to use Backbone as my framework. Below is a basic workflow that this (and most apps) follow.
What is the correct/best model to use with Backbone?
Old Way
User navigates to a page.
Selects "Create New widget"
User is presented with a form filled with inputs
At this point I would probably take the values entered (after passing basic validation), wrap them up and send them to the server via an ajax request
Request comes back as "OK" and the user is taken somewhere else (This step isn't entirely important)
Some basic pseudo-code
// Grab values
var userName = $('.UserName').val(),
dateOfBirth = $('.DateOfBirth').val();
...
...
...
$.ajax({
url: "/Webservices/ProcessStuff",
success: function(result){
if (result) {
// Render something or doing something else
} else {
// Error message
}
},
error: function () {
// Error message
}
});
Backbone way
Using the same example as above; I assume I'd have a model for the user information and a view to display the inputs. However, processing the actual call to the web service is one of the things I'm confused about. Where does this need to go? In the model or in the view click
of some "Go" button?
Model.UserInformation = Backbone.Model.extend({ username: null, dateOfBirth: null });
Maybe also have a collection of these UserInformation models?UserInformations = Backbone.Collection.extend({ model: Model.UserInformation' });
So bottom line what I'm asking is...
What is the best way to achieve this functionality?
What is the proper way to actually perform CRUD? Where to put the actual call to delete/update/create/etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有正确的想法,Backbone 应该让您可以使用相同的基本工作流程高级概述轻松完成工作。请注意,您仍将使用 jQuery 来实现此功能 - 您只需通过 Backbone 类型的组织方面来完成此操作。
您需要几个关键项目,其中大部分您已经提到过:
我认为您唯一缺少的是模型上有一个
save
方法,它包含了所有逻辑调用您的创建/更新路由后端服务器。该模型还有一个delete
方法来处理从服务器的删除。作为一个非常简单的示例,这里有一个表单,它将 HTML 模板呈现到屏幕上,将用户输入收集到模型中,然后将其保存到服务器。
HTML 模板:
运行该模板的代码:
这将使您快速启动将新模型保存回服务器的表单。
您的服务器需要执行以下操作:
id
字段。您的服务器执行这些操作并在响应中包含
id
字段非常重要。如果没有来自服务器的id
字段,当您再次调用save
时,您的模型将永远无法更新自身。它只会尝试再次在服务器上创建一个新实例。Backbone 使用模型的 id 属性来确定在将数据推送到后端时是否应该创建或更新模型。创建新模型和保存模型之间的区别仅在于 id 属性。无论模型是新模型还是编辑后的模型,您都可以对模型调用
save
。删除的工作方式相同 - 您只需在模型上调用
destroy
,它就会回调服务器来进行销毁。对于某些具有“删除”链接或按钮的 HTML,您可以附加到该 HTML 元素的单击事件,就像我为“保存”按钮显示的那样。然后,在删除点击的回调方法中,您将调用 this.model.destroy() 并传递所需的任何参数,例如成功和错误回调。请注意,我还在模型上包含了一个
urlRoot
。如果模型不属于集合,则模型上需要此函数或url
函数。如果模型是集合的一部分,则集合必须指定url
。我希望这有帮助。
You have the right idea and Backbone should make it easy for you to get things done using the same basic high level overview of your workflow. Note that you're still going to be using jQuery for this functionality - you'll just be doing it through the organizational aspects of Backbone's types.
There are a couple of key items that you'll want in place, most of which you already mentioned:
I think the only thing you are missing is that the model has a
save
method on it, which wraps up all of the logic to call the create / update routes on your back-end server. The model also has adelete
method to handle deletion from the server.As a very simple example, here's a form that renders an HTML template to the screen, gathers the user input in to the model and then saves it to the server.
An HTML template:
The code to run this:
This will give you a quick start for a form that saves a new model back to the server.
There are a couple of things your server needs to do:
id
field.It's very important that your server do these things and include an
id
field in the response. Without anid
field from the server, your model will never be able to update itself when you callsave
again. It will only try to create a new instance on the server again.Backbone uses the
id
attribute of a model to determine if it should create or update a model when pushing data to the back end. The difference between creating a new model and saving one is only theid
attribute. You callsave
on the model whether it's a new or an edited model.A delete works the same way - you just call
destroy
on the model and it does a call back to the server to do the destroy. With some HTML that has a "delete" link or button, you would attach to the click event of that HTML element the same as I've shown for the "Save" button. Then in the callback method for the delete click, you would callthis.model.destroy()
and pass any parameters you want, such as success and error callbacks.Note that I included a
urlRoot
on the model, as well. This, or aurl
function are needed on a model if the model is not part of a collection. If the model is part of a collection, the collection must specify theurl
.I hope that helps.
如果视图的“el”是表单标签,那么您可能可以使用内置事件对象来绑定要提交的函数,但如果视图的根是其他东西,那么您需要附加单击渲染函数中的处理程序。
If the "el" of the view is the form tag, then you could probably use the built in event object to bind a function to submit, but if the root of the view is something else, then you'll need to attach the click handler in the render function.