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. bzoj 3837 (随机过题法了解一下)

    3837: [Pa2013]Filary Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 395  Solved: 74[Submit][Status] ...

  2. 【拉格朗日插值法】【找规律】【高精度】Gym - 101156G - Non-Attacking Queens

    题意:问你n*n的国际象棋棋盘上放3个互不攻击皇后的方案数. oeis……公式见代码内 //a(n) = 5a(n - 1) - 8a(n - 2) + 14a(n - 4) - 14a(n - 5) ...

  3. [java][jboss]改变jboss部署目录(虚拟目录)

    原文: [java][jboss]改变jboss部署目录(虚拟目录) 在开发过程中,有时候我们希望将程序放在我们的源代码目录中,比如d:\code下,而不是放在jboss的deploy下,怎么办? 我 ...

  4. http协议之 COOKIE

    cookie我们都很了解,这里描述下cookie的几个参数意义 key = "qq", value = "Bobser" .. os.time(), path ...

  5. Git_操作标签

    如果标签打错了,也可以删除: $ git tag -d v0.1 Deleted tag 'v0.1' (was e078af9) 因为创建的标签都只存储在本地,不会自动推送到远程.所以,打错的标签可 ...

  6. Spring @Value 用法小结,#与$的区别

    20161016更新:这货其实是SpEL的功能,来这里看看吧: Spring 4 官方文档学习(五)核心技术之SpEL 起因 一直的用法是 @Value("${jdbc.driverClas ...

  7. How to exit the entire application from a Python thread?

    If all your threads except the main ones are daemons, the best approach is generally thread.interrup ...

  8. onInterceptTouchEvent 与 onTouchEvent 分析与MotionEvent在ViewGroup与View中的分发

    onInterceptTouchEvent 与 onTouchEvent 分析与MotionEvent在ViewGroup与View中的分发        Notice:本文将紧接着 Android ...

  9. 数据是企业的无价財富——爱数备份存储柜server的初体验(图文)

    非常早就像上这样一套数据备份系统,每天採用原来的软件备份加手动备份的方式,总有些不是太方便的地方. 加上企业规模的不断扩大,系统的增多,业务数据也日显重要.容不得半点中断和数据丢失.这不,出于对系统数 ...

  10. SQL语句200条(转)

    //重建数据库 101, create database testdatabase;use database testdatabase; 102, create table tt1(id int, n ...