[Backbone]1. Module, View classed

Welcome to the Anatomy of Backbone.js challenges! We're going to be building a simple Appointment app for our friend, Dr. Goodparts.

So, let's get started and create an Appointment model class.

var Appointment = Backbone.Model.extend({});

 

Now that we have our Appointment model class, let's create our first instance and assign it to the appointmentvariable. Pass in a title for the appointment when creating it.

Note Check out the JS Source tab below that will display relevant code for this challenge.

//Define a object for model
var appointment = new Appointment({'title': "New appointment"});

Well look at that, we have our very first appointment. But it isn't so useful, sitting there deep down in the bowels of your browser. To display it lets first create a view class and name it AppointmentView.

//Create a View CLASS
var AppointmentView = Backbone.View.extend({});

The perfect place to put our display code now exists! Now it's time to create our first AppointmentView instance. When doing so, don't forget to include the appointment model instance we just created. Assign the instance to a variable.

var appointmentView = new AppointmentView({model: appointment });

Our AppointmentView instance is almost ready, all we have to do is go back and define the AppointmentViewrender function so we can actually create some HTML. Have the render function add an <li> tag to the top-level element of the view. Use this.model.get('title') as the content of the <li>.

var AppointmentView = Backbone.View.extend({
  render: function(){
      var html = '<li>'+this.model.get('title')+'</li>';
    $(this.el).html(html);
  }
});

Almost there, but before we display anything let's set the title of the appointment instance! Set it to any string, perhaps something you'd like Dr. Goodparts to take a look at (ex. My Backbone Hurts).

appointment.set('title', "Nice to meet you!");

Time to show Dr. Goodparts his first appointment. Render the appointmentView instance and then insert its top-level element into #app using $('#app').html().

//Show the final view
appointmentView.render(); 
$('#app').html(appointmentView.el);

 

-----Final code----

//Create a model CLASS    
var Appointment = Backbone.Model.extend({});
//Define a object for model
var appointment = new Appointment({'title': "New appointment"});
//Create a View CLASS
var AppointmentView = Backbone.View.extend({
  render: function(){
      var html = '<li>'+this.model.get('title')+'</li>';
    $(this.el).html(html);
  }
});
//create a view object, include the model instance
var appointmentView = new AppointmentView({model: appointment });
//set title
appointment.set('title', "Nice to meet you!");
//Show the final view
appointmentView.render(); 
$('#app').html(appointmentView.el);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
---------------- backbone开发经典 336页清晰完整版pdf-------------- 内容简介 · · · · · · backbone.js提供了一套web开发的框架,为复杂javascript应用程序提供一个mvc结构。 《backbone.js应用程序开发》详细介绍了如何使用backbone.js完成web应用开发。全书从了解mvc、spa和backbone.js的基本知识开始,然后着手构建3个示例应用程序;此外,backbone和grunt-bbb、jquery mobile等开发工具的配合使用,以及jasmine、qunit和sinonjs等测试解决方案。 本书的作者是知名的javascript专家、谷歌chrome团队的工程师addy osmani。本书适合于javascript程序员、web开发人员,尤其是想要学习和使用backbone.js的读者阅读参考。 如果你想使用单页应用程序(spa)模型创建前端站点,本书向你展示了如何使用backbone.js完成这类工作。你将学会使用backbone自有风格的模型-视图-控制器(mvc)架构,来创建结构化的javascript应用程序。 《backbone.js应用程序开发》先从了解mvc、spa和backbone的基本知识开始,然后着手构建示例应用程序——一个简单的todo列表应用程序、restful风格的图书应用程序、以及使用backbone和requirejs的模块化应用程序。本书的作者是谷歌chrome团队的工程师addy osmani,他还演示了框架的高级应用。 《backbone.js应用程序开发》包括以下内容: 了解backbone.js如何给客户端带来mvc方面的好处; 编写易于阅读的、结构化的和易扩展代码 ; 使用backbone.marionette和thorax扩展框架; 解决使用backbone.js时会遇到的常见问题; 使用amd和requirejs将代码进行模块化组织; 使用backbone.paginator插件为collections数据分页; 使用样板代码引导新的backbone.js应用程序; 使用jquery mobile,并解决两者之间的路由问题; 使用jasmine、qunit和sinonjs对backbone应用进行单元测试。 作者简介 · · · · · · Addy Osmani,本书的作者是知名的JavaScript专家、谷歌Chrome团队的工程师,他对JavaScript应用程序架构有着强烈的爱好,还创建了一些比较流行的项目,如TodoMVC,并且对Yeoman、Modernizr和jQuery等其他开源项目也有重要贡献。除此之外,Addy Osmani也是一位高产的博主(http://addyosmani.com/blog),同时也是O'reilly出版的《JavaScript设计模式》一书的作者。 徐涛(网名:汤姆大叔;微博:@TomXuTao),微软最有价值专家(MVP)、项目经理、软件架构师,擅长大型互联网产品的架构与设计,崇尚敏捷开发模式,熟悉设计模式、前端技术、以及各种开源产品,曾获MCP、MCSE、MCDBA、MCTS、MCITP、MCPD、PMP认证。《JavaScript编程精解》、《JavaScript启示录》译者,博客地址:Http://www.cnblogs.com/TomXu。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值