MVC + jquery Data Binding ①: List binding (III)

Source: Internet
Author: User

In addition to table binding, other cyclic lists need to be bound.

In fact, they all use templates as data. The method for saving the template is only one way to annotate it. Better methods can be used freely. This is to obtain the template. For example, you can use a JSON file to store the required template and then place it in a templates. JSON data is generated in JS or templates configured by XML. If you need to use the template directly. tempname is enough. It is too tired to write a blog. Especially when you share your thoughts with others. I will not go into details here. Paste the code

 ; (Function ($ ){
VaR templatelist = []; // stores the list of general templates.

$. FN. bindlist = function (ARGs, template, options, async ){
VaR me = this;

VaR URL, async, template, datasource;

Url = me. ATTR ("datasource ");
Async = me. ATTR ("async") = "0 ";
If (arguments. length> 0 ){
Switch (typeof (ARGs )){
Case "string ":
Url = ARGs;
Break;
Case "object ":
Datasource = ARGs;
Break;
}
}
// ARGs is JSON
Switch (arguments. Length ){
Case 1:
If (ARGs = NULL)
Return me;
If (typeof (ARGs) = "string "){
Url = ARGs;
}
Else if (typeof (ARGs) = "object "){
Url = args. url;
Async = args. async | async;
Template = args. template | template;
Options = args. Options | options;
Datasource = args. Data;
}
Break;
Case 2: // URL, async | URL, tempalte | data, template
If (typeof (arguments [0]) = "string ")
Url = arguments [0];
Else
Datasource = arguments [0];

If (typeof (arguments [1]) = "bool "){
Async = arguments [1];
Template = me. gettemplate ();
}
Break;
Case 3: // URL, template, async | URL, template, options | data, template, options
Template = template;
Options = options;
If (typeof (options) = "bool "){
Async = options;
Options = undefined;
} Break;
Case 4:
Template = template;
Options = options;
Async = async;
Break;
}


If (url = undefined | url = "") & datasource = undefined ){
Return me;
}

If (datasource = undefined ){
$. Ajax ({
Type: "Post ",
Async: async,
URL: URL,
Success: function (data ){

If (Data = "" | DATA = NULL)
Return;
Data = eval ("(" + Data + ")");

Me. bindlistdata (data. List | data, template, options );
}
});
}
Else {
Me. bindlistdata (datasource, template, options );
}
Return me;
};
$. FN. bindlistdata = function (data, template, options ){

VaR me = this;
Template = template | Me. gettemplate ();
This. Empty ();
VaR resulttemplate = replacetemplate (template, data );


VaR prepend, append, dvalue;
If (options! = Undefined ){
Prepend = options. prepend;
Append = options. append;
Dvalue = options. dvalue;
}

If (prepend! = Undefined ){
Resulttemplate = prepend + resulttemplate;
}



If (append! = Undefined ){
Resulttemplate + = append;
}

This.html (resulttemplate );

// Default value of the bound control
If (this. ATTR ("type") = "select-one "){
If (dvalue! = Undefined)
This. bindcontrol (dvalue );
Else
This [0]. selectedindex = 0;
}

Me. setlink ();

};


}) (Jquery );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.