#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 searchrange
if there is norange
, then it will search$el
.
So please remember to setrange
orel
.
And also, when remove a view, it will remove all elements in$el
just as$el.remove();
. It's important to write a rightel
. -
###view collection### view collection is also a Backbone.View. But it contains views
view collection needs a function namedcreate_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
anddata_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
andmodel_option
.
model_option
is simple. it should have one property namedmodel
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 callview[view_broad_cast_index]
to send the broadcast. default isbind_view_event
. And the function has two params.event
andoption
.option
is{child:{}, current:{}}
. ifchild
isundefined
thecurrent
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 viabind_view_options
. So it should contains :
index
: atview.index
to find the occrued views
data_bind
: atoccured_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##
-
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();
-
when remove a view, please use the
remove
function.
And please note thatremove
will remove all the elements in$el
.
##Doms##
-
doms should have properties :
data_bind
anddata_type
usuallydata_type
equal to doms' type. likeimg
,input
or ...
if you want to create a new view on this dom, setdata_type
as "view" or "view_collection".
And now , it'sdata_bind
refers to a Object =>{...}
in model or model property;
If you do this, you should set aview_map
property on your dom. the value of the property is equal to the map inBackbone.BindView.ele_view_map
. And the value ofBackbone.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