[Backbone]3. More detail on View
2014-08-01 20:45
120 查看
Change the
Make sure every
Refactor the
Dr. Goodparts is getting ready to request some big changes to our
Make sure you generate the same HTML after switching to templates.
Tip: don't forget to use
to
Dr. Goodparts is just getting the hang of this web thing and thinks it'd be a good idea to
See if you can't appease his bad idea and implement this tragic UI interaction using View events.
Phew, over the weekend you convinced Dr. Goodparts to come to his senses and allow you to change the click event to only alert when the user
Make those changes quick and deploy!
-----------Final code------------
AppointmentViewto have a top-level
litag (instead of the default
divtag).
var AppointmentView = Backbone.View.extend({tagName: 'li'});
Make sure every
AppointmentViewtop-level element is created with a class of
appointment.
var AppointmentView = Backbone.View.extend({ tagName: 'li', className: 'appointment' });
Refactor the
renderfunction below to use the improved jQuery syntax on the top-level element.
var AppointmentView = Backbone.View.extend({ render: function(){ this.$el.html('<li>' + this.model.get('title') + '</li>'); } });
Dr. Goodparts is getting ready to request some big changes to our
AppointmentView. You know that eventually the HTML it generates is going to get pretty complicated, so now is probably a good time to refactor to use a template.
Make sure you generate the same HTML after switching to templates.
Tip: don't forget to use
this.model.toJSON()in
render
var AppointmentView = Backbone.View.extend({ render: function(){ this.$el.html('<span>' + this.model.get('title') + '</span>'); } });
to
var AppointmentView = Backbone.View.extend({ template : _.template('<span><%= title %></span>'), render: function(){ var attr = this.model.toJSON(); this.$el.html(this.template(attr)); } });
Dr. Goodparts is just getting the hang of this web thing and thinks it'd be a good idea to
alertthe user the
titleof the appointment whenever they
clickon its view.
See if you can't appease his bad idea and implement this tragic UI interaction using View events.
var AppointmentView = Backbone.View.extend({ template: _.template('<span><%= title %></span>'), events: { "click": function(){alert(this.model.get('title'));} }, render: function(){ this.$el.html(this.template(this.model.toJSON())); } });
Phew, over the weekend you convinced Dr. Goodparts to come to his senses and allow you to change the click event to only alert when the user
double clickson the view.
Make those changes quick and deploy!
var AppointmentView = Backbone.View.extend({ template: _.template('<span><%= title %></span>'), events: { "dblclick span": "alertTitle" }, alertTitle: function(){ alert(this.model.get('title')); }, render: function(){ this.$el.html(this.template(this.model.toJSON())); } });
-----------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({ tagName: 'li', className: 'appointment', render: function(){ this.$el.html('<span>' + this.model.get('title') + '</span>'); } });*/ //Using a better way to create html, underscore template //Always get data from model //render the data using this.model.toJSON() function var AppointmentView = Backbone.View.extend({ template: _.template('<span><%= title %></span>'), events: { "dblclick span": "alertTitle" }, alertTitle: function(){ alert(this.model.get('title')); }, render: function(){ this.$el.html(this.template(this.model.toJSON())); } }); //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);
相关文章推荐
- UISplitViewController in portrait on iPhone shows detail VC instead of master
- XML错误信息Referenced file contains errors (http://www.springframework.org/schema/beans/spring-beans-4.0.xsd). For more information, right click on the message in the Problems View ...
- More Detail On Workspaces And SmartParts (Introduction To The CAB/SCSF Part 16)
- [Backbone]2. More detail in Models
- 对Warning: Attempt to present XXX on XXX whose view is not in the window hierarchy!的解决方案
- Android7.0中文文档(API) -- ActionMenuView.OnMenuItemClickListener
- Photon Cloud Networking: OnPhotonSerializeView Not Firing
- The specified child already has a parent. You must call removeView() on the child's parent first,
- ViewGroup之onInterceptTouchEvent方法
- ViewGroup和View中onTouchEvent和onInterceptTouchEvent相关知识学习
- ViewPager的滑动方法setOnPageChangeListener已被addOnPageChangeListener取代
- 快速理解android View的测量onMeasure()与MeasureSpec
- android,view的执行过程onDraw、onSizeChanged,onFinishInflate
- 常用但忽略的anroid知识9- 自定义View之onMeasure()
- Backbone.View
- Android UI--Android View.onMeasure方法的理解
- 关于xml中使用ImageView或ImageButton引起Missing contentDescription attribute on image 的问题
- UI: 用 UISplitViewController 显示 Master-Detail 视图 ;使用 UIPageViewController 启用分页;使用 UIPopoverController 显示弹出画面(弹出框)
- Android事件传递机制以及ViewGroup的onInterceptTouchEvent的理解
- BUG:The specified child already has a parent. You must call removeView() on the child's parent first