[Backbone] Working with forms
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的更多相关文章
- Backbone源码解析(六):观察者模式应用
卤煮在大概一年前写过backbone的源码分析,里面讲的是对一些backbone框架的方法的讲解.这几天重新看了几遍backbone的源码,才发现之前对于它的理解不够深入,只关注了它的一些部分的细节和 ...
- 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 的目的是 ...
- Wizard Framework:一个自己开发的基于Windows Forms的向导开发框架
最近因项目需要,我自己设计开发了一个基于Windows Forms的向导开发框架,目前我已经将其开源,并发布了一个NuGet安装包.比较囧的一件事是,当我发布了NuGet安装包以后,发现原来已经有一个 ...
- 使用backbone的history管理SPA应用的url
本文介绍如何使用backbone的history模块实现SPA应用里面的URL管理.SPA应用的核心在于使用无刷新的方式更改url,从而引发页面内容的改变.从实现上来看,url的管理和页面内容的管理是 ...
- Backbone中的model和collection在做save或者create操作时, 如何选择用POST还是PUT方法 ?
Model和Collection和后台的WEB server进行数据同步非常方便, 都只需要在实行里面添加一url就可以了,backbone会在model进行save或者collection进行cre ...
- Backbone.js 中的Model被Destroy后,不能触发success的一个原因
下面这段代码中, 当调用destroy时,backbone会通过model中的url,向服务端发起一个HTTP DELETE请求, 以删除后台数据库中的user数据. 成功后,会回调触发绑定到dest ...
- Backbone.js应用基础
前言: Backbone.js是一款JavaScript MVC应用框架,强制依赖于一个实用型js库underscore.js,非强制依赖于jquery:其主要组件有模型,视图,集合,路由:与后台的交 ...
- xamarin.forms新建项目android编译错误
vs2015 update3 新建的xamarin.forms项目中的android项目编译错误.提示缺少android_m2repository_r22.zip,96659D653BDE0FAEDB ...
- ASP.NET Forms 身份验证
ASP.NET Forms 身份验证 在开发过程中,我们需要做的事情包括: 1. 在 web.config 中设置 Forms 身份验证相关参数.2. 创建登录页. 登录页中的操作包括: 1. 验证用 ...
随机推荐
- pkuwc2019自闭记
窝自闭了... 所以这篇\(blog\)咕咕咕了.
- 如何成为一名优秀的CTO(首席技术官)
最近我发现很多开发人员都表示不知道如何规划职业生涯的下一个步骤.基于我们目前所处的科技泡沫现象,很多工程师都倾向于留在大型的成熟公司,或者要么a)去初创企业工作要么b)自己搞初创公司. 回顾我自己的职 ...
- mysql长连接
长连接是干嘛的: 它是做连接复用的: 在openresty中的lua-resty-mysql 里 connect方法去连接mysql时会去ngx_lua cosocket连接池中寻找是否有可用连接 ...
- python编译模块为2禁制
编译模块为2禁制yum -y install python26-setuptoolseasy_install -U setuptools# cd /usr/lib64/python2.6# easy_ ...
- Redis篇
一:下载redis 官网地址:http://redis.io/ 如果系统没有安装make,请查看mysql篇 wget http://download.redis.io/redis-stabl ...
- Quartz实现动态定时任务
一. 说明 由于最近工作要实现定时任务的执行,而且要求定时周期是不固定的,所以就用到了quartz来实现这个功能: spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持q ...
- PostgreSQL 资源
http://blog.163.com/digoal@126/blog/static/163877040201172183022203/ http://m.oschina.net/u/2426299? ...
- Error launching remote program: No such file or directory
iPhone真机调试报如下错误时,关掉Xcode,重新启动就可以了,注意是关掉Xcode,彻底关掉.Error launching remote program: No such file or di ...
- android NDK 开发环境搭建
基于 Android NDK 的学习之旅-----环境搭建 工欲善其事必先利其器 , 下面介绍下 Eclipse SDK NDK Cygwin CDT 集成开发环境的搭建. 1.Android 开发环 ...
- 80x86 CPU 的工作模式
8086/8088微处理器只有一种工作模式:实地址模式. 32为的80x86微处理器有3种工作模式:实地址模式.保护模式和虚拟8086模式. 实地址模式 对于8086/8088微处理器,实模式是它 ...