Our first step is to add a template to the AppointmentForm below. Have the template produce a form with two inputs, one for the title of the appointment and one for the name of the person on the appointment. So the "name" attributes on the inputs should be name and title, respectively. Yes, you'll have an input with name="name". YOLO.

var AppointmentForm = Backbone.View.extend({
template: _.template('<form><input type="text" name="title" /><input type="text" name="name" /></form>'),
});

Now write the render function to render the template and pass in the model attributes. Return this from the render function.

var AppointmentForm = Backbone.View.extend({
template: _.template('<form><input name="title" type="text" /><input name="name" type="text" /></form>'),
render: function(){
this.$el.html(this.template(this.model.attributes));
return this;
}
});

Update the template to use the title and name attributes from the model to fill out the value attributes of the input elements in the template.

var AppointmentForm = Backbone.View.extend({
template: _.template('<form><input name="title" type="text" value="<%= title%>"/><input name="name" type="text" value="<%= name%>"/></form>'),
render: function(){
this.$el.html(this.template(this.model.attributes));
return this;
}
});

Update the AppointmentForm view to handle the submit event on the form. Also go ahead and implement the function to handle that event. It should save both the title and name attributes on the model with values from their respective inputs. Make sure the event function stops the default event from happening (which would cause the browser to submit the form, instead of us handling it with our Backbone model.)

var AppointmentForm = Backbone.View.extend({
template: _.template('<form><input name="title" type="text" value="<%= title %>" /><input name="name" type="text" value="<%= name %>" /></form>'),
events: {
'submit': 'save'
},
render: function(){
this.$el.html(this.template(this.model.attributes));
return this;
},
save: function(e){
e.preventDefault();
var title = this.$('input[name="title"]').val();
var name = this.$('input[name="name"]').val();
this.model.save({title: title, name: name});
}
});

After submitting the form and saving the model, make sure we navigate the user back to the index route ''. Also, make sure this only happens if the model is saved successfully.

var AppointmentForm = Backbone.View.extend({
template: _.template('<form><input name="title" type="text" value="<%= title %>" /><input name="name" type="text" value="<%= name %>" /></form>'),
events: {
'submit': 'save'
},
render: function(){
this.$el.html(this.template(this.model.attributes));
return this;
},
save: function(e){
e.preventDefault();
var title = this.$('input[name="title"]').val();
var name = this.$('input[name="name"]').val();
this.model.save({title: title, name: name}, {
success: function(){
Backbone.history.navigate('', {trigger: true});
}
});

It's possible that saving the appointment on the server will fail and the server will respond with error messages. Add an error callback to the save call to handle this case and alert the user with the errors from the response.

var AppointmentForm = Backbone.View.extend({
template: _.template('<form><input name="title" type="text" value="<%= title %>" /><input name="name" type="text" value="<%= name %>" /></form>'),
render: function(){
this.$el.html(this.template(this.model.attributes));
return this;
},
events: {
submit: "save"
},
save: function(e){
e.preventDefault();
var newTitle = this.$('input[name=title]').val();
var newName = this.$('input[name=name]').val();
this.model.save({title: newTitle, name: newName}, {
success: function(){
Backbone.history.navigate('', {trigger: true});
},error: function(model, xhr, options){
var error = JSON.parse(xhr.responseText).errors;
alert(error);
}
});
}
});

[Backbone] Working with forms的更多相关文章

  1. Backbone源码解析(六):观察者模式应用

    卤煮在大概一年前写过backbone的源码分析,里面讲的是对一些backbone框架的方法的讲解.这几天重新看了几遍backbone的源码,才发现之前对于它的理解不够深入,只关注了它的一些部分的细节和 ...

  2. MVC、MVP、MVVM、Angular.js、Knockout.js、Backbone.js、React.js、Ember.js、Avalon.js、Vue.js 概念摘录

    注:文章内容都是摘录性文字,自己阅读的一些笔记,方便日后查看. MVC MVC(Model-View-Controller),M 是指业务模型,V 是指用户界面,C 则是控制器,使用 MVC 的目的是 ...

  3. Wizard Framework:一个自己开发的基于Windows Forms的向导开发框架

    最近因项目需要,我自己设计开发了一个基于Windows Forms的向导开发框架,目前我已经将其开源,并发布了一个NuGet安装包.比较囧的一件事是,当我发布了NuGet安装包以后,发现原来已经有一个 ...

  4. 使用backbone的history管理SPA应用的url

    本文介绍如何使用backbone的history模块实现SPA应用里面的URL管理.SPA应用的核心在于使用无刷新的方式更改url,从而引发页面内容的改变.从实现上来看,url的管理和页面内容的管理是 ...

  5. Backbone中的model和collection在做save或者create操作时, 如何选择用POST还是PUT方法 ?

    Model和Collection和后台的WEB server进行数据同步非常方便, 都只需要在实行里面添加一url就可以了,backbone会在model进行save或者collection进行cre ...

  6. Backbone.js 中的Model被Destroy后,不能触发success的一个原因

    下面这段代码中, 当调用destroy时,backbone会通过model中的url,向服务端发起一个HTTP DELETE请求, 以删除后台数据库中的user数据. 成功后,会回调触发绑定到dest ...

  7. Backbone.js应用基础

    前言: Backbone.js是一款JavaScript MVC应用框架,强制依赖于一个实用型js库underscore.js,非强制依赖于jquery:其主要组件有模型,视图,集合,路由:与后台的交 ...

  8. xamarin.forms新建项目android编译错误

    vs2015 update3 新建的xamarin.forms项目中的android项目编译错误.提示缺少android_m2repository_r22.zip,96659D653BDE0FAEDB ...

  9. ASP.NET Forms 身份验证

    ASP.NET Forms 身份验证 在开发过程中,我们需要做的事情包括: 1. 在 web.config 中设置 Forms 身份验证相关参数.2. 创建登录页. 登录页中的操作包括: 1. 验证用 ...

随机推荐

  1. luoguP4491 [HAOI2018]染色 广义容斥原理 + FFT

    非常明显的摆了一个NTT模数.... 题目中求恰好\(k\),那么考虑求至少\(k\) 记\(g(k)\)表示至少\(k\)中颜色出现了恰好\(S\)次 那么,\[g(k) = \binom{M}{k ...

  2. Contest Reviews(Updating)

    现在每天至少一套题又不太想写题解…… 那就开个坑总结下每场的失误和特定题目的技巧吧 2018.8.25[ZROI] T3传送门 T1:找规律找崩了…… 最好不要一上来就钻进大讨论,先想有没有普适规律 ...

  3. 某谷 P5153 简单的函数

    题面在这里 个人感觉这个题可以被打表随便艹过,当然我不是这么做的... 虽然n可达10^18,但随便分析一下就可以发现f(n)是极小的,因为f(n)一步就可以跳到f(前100),不信你算一下前100个 ...

  4. SCC缩点

    int V; //顶点数量 vector<int> G[max_v]; //图的邻接表表示方法 vector<int> rG[max_v]; //把边反向建的图 vector& ...

  5. 利用Hog特征和SVM分类器进行行人检测

    在2005年CVPR上,来自法国的研究人员Navneet Dalal 和Bill Triggs提出利用Hog进行特征提取,利用线性SVM作为分类器,从而实现行人检测.而这两位也通过大量的测试发现,Ho ...

  6. tomcat部署应用仅需ip和port访问

    一.使用ip和port访问应用项目: 打开tomcat安装根目录,打开conf目录下server.xml,找到<Host>节点,并且在该节点下新增: <Context   docBa ...

  7. 使用LM2576制作数控电源

    http://www.daxia.com/bibis/moredata30_1207792_29862.shtml 图中DA和PWM任选其一, 当DA或PWM输出为0~1.25V时,输出在12.5V~ ...

  8. strace_for_windows

    http://drmemory.org/strace_for_windows.html https://github.com/intellectualheaven/stracent/releases

  9. 用Qemu模拟vexpress-a9 (一) --- 搭建Linux kernel调试环境

    参考: http://blog.csdn.net/linyt/article/details/42504975 环境介绍: Win7 64 + Vmware 11 + ubuntu14.04 32 u ...

  10. SpreadSheet数据导出为DataTable z

    devexpress也提供了一种将excel数据,而且可以指定区域进行转换,用起来相当灵活,本人将其解决方法分享如下(代码):          private DataTable export(Wo ...