Knockout components or templates performance gains

后端 未结 2 1068
无人共我
无人共我 2021-01-27 13:48

I have an observable array. For every array element I generate some html form, very extended, as the observable array items are large objects with observables in turn:



        
2条回答
  •  灰色年华
    2021-01-27 14:35

    I made a version of your fiddle that uses components to supply the input fields. The component used is named 'my-' plus whatever the type field is (this was necessary to distinguish my components from input and select tags). I don't know how well this will perform, but it's simple enough that you should be able to do some testing and see.

    ko.components.register('my-input', {
      viewModel: InputModel,
      template: {
        element: 'input-template'
      }
    });
    ko.components.register('my-select', {
      viewModel: InputModel,
      template: {
        element: 'select-template'
      }
    });
    ko.components.register('my-mu', {
      viewModel: InputModel,
      template: {
        element: 'mu-template'
      }
    });
    
    function InputModel(params) {
      return params;
    }
    
    
    function Model() {
      records = ko.observableArray([
        [{
          type: "input",
          id: "Date",
          size: "100px",
          value: ko.observable()
        }, {
          type: "select",
          id: "Weather",
          size: "100px",
          value: ko.observable(),
          options: [{
            optId: "w1",
            optTxt: "Cloudy"
          }, {
            optId: "w2",
            optTxt: "Sunny"
          }, {
            optId: "w3",
            optTxt: "Rainy"
          }, {
            optId: "w4",
            optTxt: "Snowy"
          }, {
            optId: "w5",
            optTxt: "Foggy"
          }]
        }, {
          type: "input",
          id: "Lat",
          size: "80px",
          value: ko.observable()
        }, {
          type: "input",
          id: "Long",
          size: "80px",
          value: ko.observable()
        }],
        [{
          type: "input",
          id: "Date",
          size: "100px",
          value: ko.observable()
        }, {
          type: "select",
          id: "Temperature",
          size: "120px",
          value: ko.observable(),
          options: [{
            optId: "t0",
            optTxt: "<-10"
          }, {
            optId: "t1",
            optTxt: "]-10 : 0]"
          }, {
            optId: "t2",
            optTxt: "]0 : 20]"
          }, {
            optId: "t3",
            optTxt: "]20 : 40]"
          }, {
            optId: "t4",
            optTxt: ">40"
          }]
        }, {
          type: "select",
          id: "Wind",
          size: "70px",
          value: ko.observable(),
          options: [{
            optId: "wind1",
            optTxt: "Strong"
          }, {
            optId: "wind2",
            optTxt: "Weak"
          }]
        }],
        [{
          type: "input",
          id: "Date",
          size: "100px",
          value: ko.observable()
        }, {
          type: "select",
          id: "Weather",
          size: "100px",
          value: ko.observable(),
          options: [{
            optId: "w1",
            optTxt: "Cloudy"
          }, {
            optId: "w2",
            optTxt: "Sunny"
          }, {
            optId: "w3",
            optTxt: "Rainy"
          }, {
            optId: "w4",
            optTxt: "Snowy"
          }, {
            optId: "w5",
            optTxt: "Foggy"
          }]
        }, {
          type: "input",
          id: "Lat",
          size: "80px",
          value: ko.observable()
        }, {
          type: "input",
          id: "Long",
          size: "80px",
          value: ko.observable()
        }],
        [{
            type: "input",
            id: "Date",
            size: "100px",
            value: ko.observable()
          }, {
            type: "select",
            id: "Temperature",
            size: "120px",
            value: ko.observable(),
            options: [{
              optId: "t0",
              optTxt: "<-10"
            }, {
              optId: "t1",
              optTxt: "]-10 : 0]"
            }, {
              optId: "t2",
              optTxt: "]0 : 20]"
            }, {
              optId: "t3",
              optTxt: "]20 : 40]"
            }, {
              optId: "t4",
              optTxt: ">40"
            }]
          }, {
            type: "input",
            id: "Humidity",
            size: "70px",
            value: ko.observable(),
            options: [{
              optId: "wind1",
              optTxt: "Strong"
            }, {
              optId: "wind2",
              optTxt: "Weak"
            }]
          }, {
            type: "mu",
            id: null,
            value: '%'
          }
    
        ]
      ]);
    
    }
    var myModel = new Model();
    ko.applyBindings(myModel);
    
    
    
    
    

提交回复
热议问题