how to debug inline knockoutjs code in html - debugging

For example we have codes like :
<!-- ko ifnot: product().bannerImageVideoLink -->
<img data-bind="attr: {src: product().largeImageURLs()[0] }">
<!-- /ko -->
Is there any way to debug into the code in html so that I can see what inside product() is ?

I always use a browser add-on for this like:
Knockout context debugger extension for Chrome dev tools
FireKnockout add-on for Firefox
Install the extension, browse up to the point where you want to debug your app, select the element in the browser's dev-tools and find the current state in detail in the extension tab.

how about ko.toJSON
from http://knockoutjs.com/documentation/json-data.html
Note that ko.toJSON accepts the same arguments as JSON.stringify. For
example, it can be useful to have a “live” representation of your view
model data when debugging a Knockout application. To generate a nicely
formatted display for this purpose, you can pass the spaces argument
into ko.toJSON and bind against your view model like
run snippet below
var product = {
'foo': 'bar',
'largeImageUrls': ['image1', 'image2', 'image3', 'image4'],
'bannerImageVideoLink': true
}
function viewModel() {
var self = this;
this.product = ko.observable(ko.mapping.fromJS(product));
};
var vm = new viewModel();
$(document).ready(function() {
ko.applyBindings(vm);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script>
<!-- ko ifnot: product().bannerImageVideoLink -->
<img data-bind="attr: {src: product().largeImageURLs()[0] }">
<!-- /ko -->
debugger....
<pre data-bind="text: ko.toJSON($root, null, 2)"></pre>

The way I do this is by calling a function which uses the browsers debugger like this...
<!-- ko if: function(){debugger;}() --><!-- /ko -->
so if you add that just before your code like this...
<!-- ko if: function(){debugger;}() --><!-- /ko -->
<!-- ko ifnot: product().bannerImageVideoLink -->
<img data-bind="attr: {src: product().largeImageURLs()[0] }">
<!-- /ko -->
...then run the page with the chrome developer tools window open. Your browser will hit a breakpoint on that line. You can then inspect the $data variable in your console. This variable will contain the data being bound by knockout which in this case is the model.
You can see this in action by looking at this jsfiddle with your dev tools window open (F12)
https://jsfiddle.net/fa3x9o2s/

Related

VueJS mouseover in for loop

I have a for that will create a component for each index.
In this component, I have a child div containing edit, add, minus buttons.
I would like it to be displayed on the component mouseover.
How do I achieve this dynamically without having to play with indexes ?
Thank you kindly.
Post component
<template>
<div v-on:mouseleave.native="showOperations = false"
v-on:mouseover.native="showOperations = true">
<!-- post data -->
<div v-if="showOperations">
<!-- operations -->
</div>
</div>
</template>
<script>
export default {
...
data () {
return {
showOperations: false
}
},
...
</script>
List of post
<post v-for="post in posts"
:key="post.id"
:post="post">
</post>
This pattern works for me and I think it works for you as well

Binding to paper-dropdown-menu with Aurelia

I am wanting to provide a dynamic set of items to a Polymer paper-dropdown-menu. I can get the dynamic items to show up, but clicking them does nothing.
View:
<paper-dropdown-menu label="Your favourite pastry">
<paper-menu class="dropdown-content" selected.two-way="favPastry" attr-for-selected="value">
<!-- Begin works when selected -->
<paper-item value="Croissant">Croissant</paper-item>
<paper-item value="Donut">Donut</paper-item>
<paper-item value="Financier">Financier</paper-item>
<paper-item value="Madeleine">Madeleine</paper-item>
<!-- End works when selected -->
<!-- Begin does not work when selected -->
<paper-item repeat.for="pastry of pastries" value="${pastry}">${pastry}</paper-item>
<!-- End does not work when selected -->
</paper-menu>
</paper-dropdown-menu>
View-model:
pastries = [ "Bear Claw", "Pie" ];
favPastry = "Donut";
How can I provide a dynamic list of items to a paper-dropdown-menu in an Aurelia application?
To create the paper-items out of your array, you'll have to use a <template is="dom-repeat"> as described here: https://www.polymer-project.org/1.0/docs/devguide/templates.html#dom-repeat

wowhead_tooltips inside Angular ng-repeat

I'm using Laravel 5 with angularjs Trying to loop through jsonp reply showing World of Warcraft wowhead_tooltips and have come to a problem I cannot decipher. Snippets included below.
wowhead script retrieves the item name and displays a tootip template for wow items. ng-repeat prevents the item name to be displayed, if I insert #{{topic.itemId}} the item number is displayed and the tooltip works wihtout the item name.
The exact html block with an actual item number outside ng-repeat
works fine. So the problem is 100% to do with ng-repeat, how can I get
around this ?
Laravel blade file Tooltip works outside of ng-repeat fails inside.
<SECTION id="feeds" ng-app>
<!-- Test outside loop Works Fine -->
<!-- Works ! -->
<div ng-controller="FeedsController">
<p>Guild Achievements: <b>#{{ guild_feed.achievementPoints }}</b></p> <!-- Works ! -->
<div ng-repeat="topic in news">
<div ng-switch="topic.type">
<div ng-switch-when="itemCraft">
#{{topic.character}} Crafted:
<!-- Displays nothing, however if I place #{{topic.itemId}} enclosed in a tag only the item number displays and the tootip works, uunlike above though no item name is displayed from WoWhead. -->
<hr/>
</div>
<div ng-switch-when="itemLoot">
{{--#{{topic.character}} Looted: #{{topic.itemId}}--}}
<p></p>
<hr/>
</div>
... Rest of code
Typical wowhead script (Works fine)
<!-- Wowhead Tooltip Scripts -->
<script type="text/javascript" src="http://static.wowhead.com/widgets/power.js"></script>
<script>
var wowhead_tooltips = {
"colorlinks": true,
"iconizelinks": true,
"renamelinks": true
}
</script>
My Angular js file to rereive list (Works fine)
function FeedsController($scope, $http) {
$http.jsonp("https://us.api.battle.net/wow/guild/dreadmaul/sons%20of%20anarchy?fields=news&locale=en_US&jsonp=JSON_CALLBACK&apikey=MYKEY").
success(function(data) {
$scope.guild_feed = data;
$scope.news = data.news;
});
}

Dynamic Templates with KnockoutJs and ASP.NET MVC3

I have a rather large ASP.NET MVC web application which uses KnockoutJS extensively throughout. Over the years I've created many templates for the application, however they all reside in various web pages using named script tags. It's almost become unbearable to manage them on the various views. I'd like to figure out a way to consolidate these templates into their own html files so that they are more manageable.
I'd like to know if there are any libraries out there that supports this concept? I don't want to reinvent the wheel, as I'm sure someone has already ran into this problem in the past and solved it. Here is a quick overview of what I'm dealing with...
Basically, I have a lot of content that resembles the following markup. Notice that I have my templates defined in the same page as my actual content markup:
[[ HOME/INDEX.CSHTML ]]
<h1>Customers</h1>
<div data-bind="template: {name: 'personTmpl', foreach: customers}"></div>
<h1>Products</h1>
<div data-bind="template: {name: 'productTmpl', foreach: products}"></div>
<script type="text/html" id="personTmpl">
Name: <span data-bind="text: name" />
Address: <span data-bind="text: address" />
</script>
<script type="text/html" id="productTmpl">
Description: <span data-bind="text: description" />
Category: <span data-bind="text: category" />
</script>
<script type="text/javascript">
$(function(){
var json = {
customers: [ { name: 'Joe', address: 'Denver, CO' } ],
products: [ { name: 'Skis', category: 'Outdoors' } ]
};
var vm = new CustomViewModel(json);
ko.applyBindings(vm);
});
</script>
[[ END HOME/INDEX.CSHTML ]]
What I'd like to do is store the personTmpl and productTmpl in their own html file and pull them into the view as needed. This would allow me to only have the content markup in my cshtml file. Plus it would enable me to use the templates from anywhere (ie. Customers\Index, Products\Show, Home\Index, etc..).
I would expect that it would require some custom js on each page, but I think this is a small price to pay for cleaning up the clutter and making my templates available to all views. I could see me using some server side tags on each page or something like this (merely an example):
#section templates {
#Content.Template("Person.tmpl", Url)
#Content.Template("Product.tmpl", Url)
}
<h1>Customers</h1>
<div data-bind="template: {name: 'personTmpl', foreach: customers}"></div>
<h1>Products</h1>
<div data-bind="template: {name: 'productTmpl', foreach: products}"></div>
<script type="text/javascript">
$(function(){
var json = #Html.Raw(Json.Encode(ViewBag.PageData)));
var vm = new CustomViewModel(json);
ko.applyBindings(vm);
});
</script>
With storing them into their own templates, I could even query the content dynamically for tooltips and dialogs using old fashion $.get('/tmpl/Person.tmpl', renderFunc).
Again, I don't mind creating my own implementation, but I'm convinced there is a solution out there already. Anybody?
Thanks in advance!!
My recommendation would be to look at the external template engine here: https://github.com/ifandelse/Knockout.js-External-Template-Engine
It allows you to place your templates in external files, reference them in your binding just as you normally would using the name parameter, and uses some conventions or configuration to determine the exact path to find the template file.
The external template engine is a pretty robust solution. I have also recently been using require.js with its text plugin for this purpose as well. More info in this answer: knockout.js loading templates at runtime
If you want to render them in-line, then I suppose a helper function could load the file and wrap it in a script tag with a non-JS type.

Knockout.js 1.3.0 IF statement syntax when using containerless control flow

Here's example from Steve Sanderson's blog demonstrating a containerless IF statement in knockout:
<h3>Products</h3>
<ul>
<li><strong>Here is a static header item</strong></li>
<!-- ko foreach: products -->
<li>
<em data-bind="text: name"></em>
<!-- ko if: manufacturer -->
— made by <span data-bind="text: manufacturer.company"></span>
<!-- /ko -->
</li>
<!-- /ko -->
</ul>
How would I make the IF statement more complex. I am trying the following and it doesn't work (always returns false):
<!-- ko if: PlanStateName == 'Draft' -->
<div>This plan is a draft!</div>
<!-- /ko -->
How would one accomplish this?
So it turns out I made a rookie mistake. Here's the working code:
<!-- ko if: PlanStateName() == 'Draft' -->
<div>This plan is a draft!</div>
<!-- /ko -->
Since the variables are wrappered by knockout, the parentheses on PlanStateName are required to access the underlying data.
You need to surround the logical statement with {}.
See http://jsfiddle.net/photo_tom/nvYdf/55/

Resources