knockout.js

How to access $parent or $parents[] in knockout viewmodel click event?

自作多情 提交于 2019-12-22 01:27:16
问题 I've got a situation where I'd like to notify a grandparent or $parents[1] of a click event that occurs in a sub viewmodel. So basically I'd like to be able to do this self.$parents[1].actionTaken I think this doesn't work because of binding context vs viewModel but I'd like to hear if anyone has an ideas on the correct way to do something like this. Thanks self.save = function () { //do stuff to self first, then self.$parents[1].actionTaken(); }; 回答1: Alternatively, you can pass parameters

Durandal dialog

本小妞迷上赌 提交于 2019-12-22 01:16:54
问题 Please help. I'm trying to create a knockout template for durandal's dialog plugin. Is there anyone who can give me a basic example for this. Below is my sample code. But I can't make it work.. (function (require) { var app = require('durandal/app'), unitofwork = require('services/unitofwork'), dialog = require('plugins/dialog'); var self = this; var uow = unitofwork.create(); var userlist = ko.observableArray(); var selecteduser = ko.observable(); ko.dialog = { // Defines a view model class

Knockout JS - CSS Binding with dash in class name

自闭症网瘾萝莉.ら 提交于 2019-12-22 01:13:28
问题 I have a data binding in Knockout to apply a CSS class if a condition is true. When I use a dash in the class name (such as test-class) then I get a javascript error. Here is a fiddle that demonstrates the problem: http://jsfiddle.net/sgvem/2/ <p data-bind="text: property, css: { with-dash: property().length > 0 }"></p> Is there a way to add a class with a dash using Knockout JS? 回答1: Just put it in quotes: <p data-bind="text: property, css: { 'with-dash': property().length > 0 }"></p> Here's

Widget binding with Gridster and Knockout

房东的猫 提交于 2019-12-22 00:19:24
问题 I am new to Javascript and trying to use Gridster with Knockout. I have a database with items, and I use knockout foreach to bind them to a UL. The UL is styled with the Gridster library. Everything works great unless I try to add additional elements to the UL via the ObservableArray in the viewmodel. Can anyone help me understand the scope and order of operations here? It feels like the Gridster library isn't doing its styling to the new widgets. This jsfiddle shows a working demo of the

Knockout js checkbox checked binding

柔情痞子 提交于 2019-12-21 22:32:47
问题 In knockout js am trying to perform a foreach on an array of data to display check boxes. The issue I am having is that the checked databind does not appear to run until I interact with one of the boxes. For example, below I am generating 5 text boxes none of which are showing up as checked. However when I click "one", "two" and "four" also get checked as they should have been from the beginning. Javascript: var viewModel = {}; viewModel.choices = ["one", "two", "three", "four", "five"];

How replace data-bind tag with {{}} style bindings in KnockoutJS?

故事扮演 提交于 2019-12-21 22:01:21
问题 I want to declare my bindings as with AngularJS, but I'm using the KnockoutJS library. That is, I want to use Knockout but with the syntax I know from Angular. E.g.: Today's message is: <span data-bind="visible:true,text:myMessage"></span> I want to write using {{}} notation, like so: Today's message is: <span {{visible:true,text:myMessage}}></span> How can I replace data-bind attributes with {{}} based syntax in KnockoutJS? 回答1: http://mbest.github.io/knockout.punches/ is all you need.

Using Knockout mapping for complex JSON

纵饮孤独 提交于 2019-12-21 20:55:44
问题 Most of Knockout seems very intuitive. One thing that is strange to me though is how the mapping plugin works. I was expecting/hoping I would be able to feed it JSON from an ajax call, and have a sort of "dynamic" view model that is available to reference in my HTML. The description of the mapping plugin even makes it sound like this is how it works: "If your data structures become more complex (e.g. they contain children or contain arrays) this becomes very cumbersome to handle manually.

MVC validation lost in Knockoutjs post

二次信任 提交于 2019-12-21 20:54:56
问题 I'm using MVC4 and knockout. I have a form on a page that is strongly typed to a viewmodel. In that viewmodel I have some validation defined, for instance: [Required(ErrorMessage = "Title is required")] public string Title { get; set; } If I do a standard post to the form without the required field filled, my action sees that the model is not valid and returns to the view, and a main validation message is displayed because I have @Html.ValidationSummary in my form. The individual field is

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