knockout-2.0

Knockout 'for' binding?

最后都变了- 提交于 2019-12-23 07:22:08
问题 Original Question Does any one have or know of a binding for knockout that would allow behavior similar to a for loop? I can make a foreach do what I want but it would be nice if I didn't have to do it that way. Edit 2 I am trying to create table rows based on a selection the user makes. In some cases I need x rows where x is the length of an array, other times x represents the largest number rows that will be needed to display n number of arrays. Ex: image1 is built based on 4 different

Using a variable to store a knockout template

谁都会走 提交于 2019-12-22 23:24:16
问题 New to knockout and loving it so far cut a 700 line jQuery mess into 150 lines. The one part I am not really liking is the templating. I want to be able to create a file similar to this module.ViewModel.views = { 'view1' : '<div data-bind="foreach: data">TEMPLATE</div>' }; // in my view model set something like ViewModel.view1Template = module.ViewModel.views.view1; // then in my html have <div data-bind="template: view1Template()"></div> I would like to be able to do this possibly with

Using a variable to store a knockout template

只谈情不闲聊 提交于 2019-12-22 23:23:53
问题 New to knockout and loving it so far cut a 700 line jQuery mess into 150 lines. The one part I am not really liking is the templating. I want to be able to create a file similar to this module.ViewModel.views = { 'view1' : '<div data-bind="foreach: data">TEMPLATE</div>' }; // in my view model set something like ViewModel.view1Template = module.ViewModel.views.view1; // then in my html have <div data-bind="template: view1Template()"></div> I would like to be able to do this possibly with

Knockout JS + Bootstrap + Icons + html binding

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 06:59:16
问题 ok, this one is driving me nuts... I just can't seem to figure out the correct way to make html bindings in knockout, play nicely with twitter bootstrap elements. I have, the following HTML: <li><a href="#"><i class="icon-user"></i> Enable/Disable User</a></li> This line is actually part of some other li's that are in a ul, but I'm showing ONLY the bit I need for simplicity. As you can see, I'm also using twitter bootstrap here, as is evidenced by the icon class. Ok, so that's all good, when

KnockOut Mapping Hierarchical JS object

*爱你&永不变心* 提交于 2019-12-21 20:43:13
问题 I am trying to create view Models using KnockOut mapping plugin , This is the object , Basically below is a sentence with words in it. var data = { name: 'Example Title', sentences: [ {id: 1, words: [{text: 'word1'}, {text: 'word2'}]}, {id: 2, words: [{text: 'word3'}, {text: 'word4'}]} ]}; I would like to have three view Models , Article should contain sentences , an Sentence should contain words var ArticleViewModel = function(data) { var self = this; self.id = ko.observable(data.id); self

KnockOut Mapping Hierarchical JS object

空扰寡人 提交于 2019-12-21 20:41:52
问题 I am trying to create view Models using KnockOut mapping plugin , This is the object , Basically below is a sentence with words in it. var data = { name: 'Example Title', sentences: [ {id: 1, words: [{text: 'word1'}, {text: 'word2'}]}, {id: 2, words: [{text: 'word3'}, {text: 'word4'}]} ]}; I would like to have three view Models , Article should contain sentences , an Sentence should contain words var ArticleViewModel = function(data) { var self = this; self.id = ko.observable(data.id); self

Jquery knockout: Render template in-memory

微笑、不失礼 提交于 2019-12-21 12:27:53
问题 I have a knockout template: <script id="draggableHelper" type="text/x-jquery-tmpl"> <div class="draggableHelper"> <span data-bind="text: Name"></span> </div> </script> Is possible to generate the result of the template, and save it into memory, by sending the object to populate the template? Something like: var result = ko.renderTemplate($("#draggableHelper").html(), { Name: "Test" }); 回答1: Yes it's possible to apply bindings to nodes unattached to the DOM. Just use very useful function ko

knockout - HTML href

泪湿孤枕 提交于 2019-12-21 06:56:30
问题 I have a foreach loop that goes through an array (filesObservableArray). The array has a key/value with the key: URLPath. When I bind the array within the HTML, I would like to set the 'href=' value with the URLPath. I know this part is a fail, but conceptually, can you see what I'm trying to do? href="< span data-bind='text: URLPath'>" Or maybe I can use a 'databind="click: someCode(url)"' and within the someCode function, open the link? The url maps to either a document file (e.g., .doc) or

knockoutjs get element id through click event

假装没事ソ 提交于 2019-12-21 03:16:13
问题 I'm using knockoutjs and I currently have something in my view that looks like this: <img id="myTab1" data-bind="click: pressedTab.bind($data, '#myTab1')" src="images/image1.png"></img> This allows me to get the element ID in my view model: pressedTab = function(tab){ console.log("Element ID: " + tab); } This writes: Element ID: #myTab1 However, it's too repetitive to send the name of the img id in the click event. Is there a way to send the img id without explicitly re-writing it? 回答1: You

Initialize Knockout observable from element attribute value

為{幸葍}努か 提交于 2019-12-20 12:04:48
问题 I have an element that possesses an attribute whose value is bound to a knockout observable: <text transform='matrix(1 0 0 1 1 1)' data-bind='attr:{transform:textTransform}'></text> When the element loads I'd like the observable to contain the value defined on the dom element, however it instead loads as undefined and the attribute is removed from the dom element alltogether: <text data-bind='attr:{transform:textTransform}'></text> Is it possible to initialize the value of a knockout