knockoutjs-About Components

Source: Internet
Author: User

Components can be presented as widgets, which contains its own independent view, can be preloaded or loaded asynchronously, can accept parameters and return results, can inherit from others, and can be reused. By using components, we can use a custom semantic tag instead of <DIV&GT, which is also compatible under the old browser. A component contains ViewModel and template, so you need to register the component in a way like this when you use it:
1 ko.components.register (' Component-name ' , {2    viewmodel:something ...,3 4});      
where the component name is used in lowercase words, and in the form of short-term connections, it should be used as an element;ViewModel is optional, and if not, the component is equivalent to a piece of HTML code;the template is essential.
    • ViewModel can be built in a variety of ways
1 constructors in this way, an instance is created for each component individually, each with a separate ViewModel object. 2 ways to share object instances if you want multiple components to share an instance of the same ViewModel, you can use the following method:
1  var shareviewmodelinstance = {...}; 2  Ko.components.register (' my-component ' , {3     ViewModel: {instance:shareviewmodelinstance },4     Template: ... 5  });
3 Factory function mode some operations are performed on the associated elements before binding to ViewModel
1 ko.components.register (' my-component ' , {2    viewModel: {3         function (params, componentinfo) {4             return New Myviewmodel (params); 5         }     6    },7    Template: ... 8 });        
4 file load mode similar to using Require.js file
1 Ko.components.register (2     ViewModel: {require: ' Some/module/name ' },3     Template: ... 4 )
You can use any of the above schema formats in the loaded file:
1 function (KO) {2       function Myviewmodel () {3               ... 4       }5       return  Myviewmodel; 6       // return {instance:new myviewmodel ()}; 7       // ... 8 });

    • How template is built
1 using IDs to invoke templates
1 <  id= ' component-template '>2      <H1 >... </ H1 > 3 </ Template >
1 ko.components.register (' my-component ', {2      Template: {element: ' Component-template '},3     ViewModel: ... 4 });
where the <template> element is ignored by the browser, the elements inside the,<template> are considered to be part of the template.   2 use var elementinstance = document.getElementById (' component-template ')Get a string of template 3 using tagsTemplate: '

4 Creating 5 external ingest files using an array of DOM nodes

     template:{require: '/some/template '}     ' text!path/file.html '}

    • Set the component synchronous or asynchronous Load mode

Synchronous: true

    The default is false, asynchronously loading

knockoutjs-About Components

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.