knockout.js

knockoutjs get the (real bound) element through click event

一曲冷凌霜 提交于 2020-01-03 13:33:11
问题 See this question. Except that the answer returns the child element when a child element is clicked, i.e. in the case that you bind a div. <div id="parent" data-bind="click: log">Parent Div<div id="child">Child</div></div> <script> var ViewModel = function() { this.log = function(data, event) { console.log("you clicked " + event.target.id); } }; ko.applyBindings(new ViewModel()); </script> See this fiddle I want to get the original element the click event was bound to. Any suggestions? 回答1:

Knockout-Validation Using Regular Expression to Validate a Phone Number

烈酒焚心 提交于 2020-01-03 12:34:53
问题 I am trying to add a simple regular expression validation to one of my observables using Knockout-Validation. I have the following: self.ContactPhone = ko.observable().extend({ required: true, pattern: { message: 'Invalid phone number.', params: '^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$' } }); However, no matter what I enter, it returns the message 'Invalid phone number.' Is there a certain way I need to format the expression? I've tested it using purely JavaScript and it works fine. 回答1: You need

on Change event in select with knockout

岁酱吖の 提交于 2020-01-03 11:03:07
问题 I have a problem how to call onchanges knock js to my select option, i already have a function and html, but when i choose the select option, nothing changes <select data-bind="event:{change:setSelectedStation() }, options: seedData, optionsText: 'text', optionsValue: 'value'"> </select> here is my function setSelectedStation: function(element, KioskId){ this.getPopUp().closeModal(); $('.selected-station').html(element); $('[name="popstation_detail"]').val(element); $('[name="popstation

How to disable click event using Knockout?

放肆的年华 提交于 2020-01-03 09:29:12
问题 I have two buttons, which is called <a href='#' data-bind='click: clickActivateSpatialSearch' id='draw_polygon'> <a href='#' data-bind='click: clickActivateSpatialSearchBox' id='draw_box'> What will be the best here? Can I use jQuery on $(document).ready ? The problem is data-bind click disables the other click event when being pressed and likewise. But when I press same button, it enables the second button back once again. So what I'm trying to say with all jibberish is that, I only want one

How to disable click event using Knockout?

你说的曾经没有我的故事 提交于 2020-01-03 09:29:08
问题 I have two buttons, which is called <a href='#' data-bind='click: clickActivateSpatialSearch' id='draw_polygon'> <a href='#' data-bind='click: clickActivateSpatialSearchBox' id='draw_box'> What will be the best here? Can I use jQuery on $(document).ready ? The problem is data-bind click disables the other click event when being pressed and likewise. But when I press same button, it enables the second button back once again. So what I'm trying to say with all jibberish is that, I only want one

Validation in Breeze and Knockout

拜拜、爱过 提交于 2020-01-03 08:41:36
问题 I've recently added Breeze to a project using Knockout and ASP.NET MVC4. I really like Breeze and it saves a lot of coding. I have used Knockout validation and like how it validates a property field text box on data entry and the way the error message is displayed. I would like to use a combination where the validation requirements are defined in the back-end with C# attributes on the object properties, the validation requirements are received in the metadata and then Knockout validation can

Apply a class to every nth template element with knockoutjs

强颜欢笑 提交于 2020-01-03 06:31:12
问题 I've been experimenting with knockoutjs, reworking an existing project. My current layout has the logic to determine if the rendered <div> tag is the first or fourth item. if (index % 4 == 0) addClass('omega'); if (index % 4 == 1) addClass('alpha'); Is there any built-in functionality of knockout that can template similar conditions? 回答1: A few options for you: there is work being done to add a $index variable when doing a foreach in KO. This is scheduled to be included in KO 2.1. The pull

Knockout.js force change after JavaScript changes a textbox

烂漫一生 提交于 2020-01-03 04:38:10
问题 I have an input: <input id="MyTextBox" type="text" data-bind="value: MyTextBox" /> If I change the textbox myself then data binding works... but if I set the text of the textbox with JQuery the textbox changes but no data bind occurs. $("#MyTextBox").val("Some text"); How can I change a textbox without any keystrokes and still get binding to work? Thank you 回答1: When using Knockout, you generally shouldn't be using something else to modify the view. So instead of using jQuery to change the

knockout.js mapping causing infinite recursion on IE9

你离开我真会死。 提交于 2020-01-03 02:28:08
问题 I'm having endless recursion on IE by my knockout.js mapping. Can someone more familiar with KO to spot what am I doing wrong? I have following knockout.js mapping: var mapping = { create: function(options) { return new MyViewModel(options.data); }, 'ChildItems': { create: function(options) { return new ChildVM(options.data); } } } When I render the page approx. 1 time out of 5 IE ends up with following infinite recursion stack(causing "SCRIPT28: Out of stack space"). IE's callstack: fromJS

KnockoutJS/Bootstrap - Clearing modal form when closing modal using javascript

百般思念 提交于 2020-01-02 16:34:13
问题 I have a Bootstrap Modal that contains a form for updating or creating an entity (Company in my example). Right now my issue is that if I view an entity using the modal, it doesn't clear out the fields when I close the modal by any means. Causing the form to still be populated if I then click a "Create" button, which should bring me up a blank modal. How can I execute one of my ViewModels methods from just regular javascript? Here is some of my code: function ViewModel() { var self = this;