The View in Backbone is used to display data transmitted from the Model layer, or some data generated in the View, including data input in the input box, and is passed from the current View to another View layer. What should I do? I read before that a blogger "Three Communication Methods of Backbone View" wrote it particularly clearly. In my actual projects, the last method is often used.
Hehe, sharing knowledge is a happy thing, so I will directly refer to it as follows:
Use Backbone directly as the event registration machine.
The code is as follows:
var ApplicationView = Backbone.View.extend({initialize : function(){this.documentView = new DocumentView();this.sidebarView = new SidebarView();},});var DocumentView = Backbone.View.extend({onEdit : function(){Backbone.trigger('documentEdit');}});var SidebarView = Backbone.View.extend({initialize : function(options){Backbone.on('documentEdit', this.onDocumentEdit, this);},onDocumentEdit : function(){// react to document edit.}});Using the Backbone time registering machine method can not only realize the information transmission between different child views under the same parent view, but also realize the information transmission of each child view under different parent view. When I first came into contact, I felt that this method was particularly useful, but later there were two things that I was a little confused about, and I would like to share them together.
The first is when we pass information or data from the parent View to the child View, it should be OK to register with event. However, in the project, I use the information to be passed in the parent View when initializing the view, so that after the view is rendered, the data given to it by the parent View is already there. Just like this:
This.receive is packaged by the parent View. When initializing the child View, just post this.receive.
The second case is the transmission between the same child view. Everyone may understand this case. I'll explain it. It is to define a global variable for this view in a child view, and then refer to this variable through this.
The above is the learning experience of passing values between Views in Backbone introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!