Skip to content

Latest commit

 

History

History
163 lines (136 loc) · 4.82 KB

README.md

File metadata and controls

163 lines (136 loc) · 4.82 KB

#Backbone-TowWayBind Manual# ##Description##

  • ###model and collection### just create these as usual

      var taskModel = Backbone.Model.extend({
        defaults : {
          id : "",
          task : ""
        }
      });
    
  • ###view### when create a view or initialize a view collection the TwoWayBind will search the dom to create the BindDom element.
    It will first search range if there is no range, then it will search $el.
    So please remember to set range or el.

    And also, when remove a view, it will remove all elements in $el just as $el.remove();. It's important to write a right el.

  • ###view collection### view collection is also a Backbone.View. But it contains views
    view collection needs a function named create_view(). It should have a return value which is the view you have just created. You can also change the name of this function. I will descripe it later

  • ###html### when scope the dom. all the dom that has data_type and data_bind will be warpped by BindDom.

##Functions##

  • ###Backbone.TowWayBind### this is the only function that you need to write.
    It has two params. view_potion and model_option.

    model_option is simple. it should have one property named model which contains model or collection.

    view_option is more complex.

    When it is a view. Params are :
    type : show this view's type.
    from_bind_dom: sign if it is relies on a BindDom . bool
    view : the target view to bind. required
    data_bind : the index that the view map to the model. bool
    view_broad_cast_index : when this view get a broadcast. It will call view[view_broad_cast_index] to send the broadcast. default is bind_view_event. And the function has two params. event and option. option is {child:{}, current:{}}. if child is undefined the current is the man who first broadcast the event.
    range : range is the flag to tell TwoWayBind where to search the doms if it is none, it will display as we talked.
    scope : whether to search and find element.

    When it is a view collection, some params is the same, so here only list the different

    bind_view_options : if this view has some always occur view, TwoWayBind can also scope these views and bind via bind_view_options. So it should contains :
    index : at view.index to find the occrued views
    data_bind : at occured_view.data_bind to find the data_bind.
    then they are the same. And the params are: view_broadcast_index, scope, range.

    view_create_index : if you don't like the default value, set this param to change.

##Attention##

  1. when remove a dom, you can use whatever you like. But when change a dom 's value, if it is not a input, please remember to raise change. like this.

      $(tar).siblings(".task-label").find("span").html(val);
    
      $(tar).siblings(".task-label").find("span").change();
    
  2. when remove a view, please use the remove function.
    And please note that remove will remove all the elements in $el.

##Doms##

  • doms should have properties : data_bind and data_type
    usually data_type equal to doms' type. like img, input or ...

    if you want to create a new view on this dom, set data_type as "view" or "view_collection".
    And now , it's data_bind refers to a Object =>{...} in model or model property;
    If you do this, you should set a view_map property on your dom. the value of the property is equal to the map in Backbone.BindView.ele_view_map. And the value of Backbone.BindView .ele_view_map[view_map] is the view you already created and need to bind;
    Demo like this :

     <div data_type="view" data_bind="img_content" view_map="img_view">
         ....
     </div>
     
     var iv = Backbone.View.extend({
        .....
     });
     
     
     var viewUnit = Backbone.View.extend({
        ......
        
        initialize : function(..){
           /*---create doms ----*/
           var img_view = new iv(..);
           Backbone.BindView.ele_view_map[img_view] = img_view;
        }
     });
     
     var viewCollection = Backbone.View.extend({
         ......
         
         create_view : function(){
            return new viewUnit();
         }
     
     });
     
     var view_collection = new viewCollection(...);
     
     var twb = Backbone.TwoWayBind(....); //bind view_collection
    

###end###

  • by redhome-xiaoqi