Note that there are some explanatory texts on larger screens.

plurals
  1. POExtend entity or add ko.computed to return additional property in entity that is not in database
    primarykey
    data
    text
    <p>I can't quite get my head around how to do this. I've started with Julie Lerman's "BreezyDevices" solution to learn with and I have used her javascript viewmodel as a base.</p> <p>I have:</p> <pre><code>//properties and methods to expose via this class var vm = { game: ko.observableArray([]), save: function () { dataservice.saveChanges(); }, reset: function () { dataservice.reset(getAllGames) }, }; </code></pre> <p>at the top of the viewmodel and this will return each of my games in an array. All works fine. "Games" has related data which returns an array called "Sets" containing "ourscore" and "theirscore" as properties.</p> <p>On my html page I want to bind the concrete database properties returned as part of the "game" entity but I also want to create a "result" property for each game which is computed based on a javascript function that loops through each set score and returns values accordingly.</p> <p>I tried using the layout in the breeze "Todo" solution and set this up immediately under the code above:</p> <pre><code>initVm(); function initVm() { addComputeds(); } function addComputeds() { vm.result = ko.computed(function () { var ourSets = getResult().ourSets; var theirSets = getResult().theirSets; if (ourSets == 0 &amp;&amp; theirSets == 0) { return "No Result"; } return (ourSets &gt; theirSets ? "Won " : "Lost ") + "&lt;b&gt;" + ourSets.toString + "&lt;/b&gt;-" + theirSets.ToString; }); } function getResult() { var ourSets = 0; var theirSets = 0; vm.game().forEach(function (game) { for (var gs in game.Sets) { if (gs.ourScore &gt; gs.theirScore) { ourSets +=1; } else { theirSets +=1; } } }); return { ourSets: ourSets, theirSets: theirSets }; } </code></pre> <p>but that seems to me as though it'll add a "result" to the viewmodel (vm) and not each game entity? Also when I run the code it doesn't error, but it doesn't create a "result" property anywhere I can see and just doesn't appear to be working.</p> <p>Looking at it again as I add it in here I can see it's wrong as it needs to be dealing with each specific game entity to work out each result and not the array of games (so I need something in vm.games.result and not vm.result) but I am too new at this to understand how to address each individual game entity. My .net coding brain would have me pass each game entity in a loop to a function to return the result for that game but I don't know if that's how it works with breeze/knockout too.</p> <p>I've googled everywhere but I just can't seem to find the relevant examples for my requirement so would really appreciate some pointers please!</p> <hr> <p>@BeaverProj</p> <p>I have a main.js file in which this happens:</p> <pre><code>(function (root) { var app = root.app; app.logger.info('Please wait... data loading'); ko.applyBindings(app.gameViewModel, $("content").get(0)); $(".view").css({ display: 'block' }); }(window)); </code></pre> <p>Have now edited top section to this:</p> <pre><code>var vm = { game: ko.observableArray([]), save: function () { dataservice.saveChanges(); }, reset: function () { dataservice.reset(getAllGames) }, result: ko.computed(function () { var gameRes = getResult(); var ourSets = gameRes.ourSets; var theirSets = gameRes.theirSets; if (ourSets == 0 &amp;&amp; theirSets == 0) { return "No Result"; } return (ourSets &gt; theirSets ? "Won " : "Lost ") + "&lt;b&gt;" + ourSets + "&lt;/b&gt;-" + theirSets; }) }; </code></pre> <p>"getResult" now references "app.gameViewModel.game().forEach(function (Game) {" instead of "vm..."</p> <p>As before - no errors, but no results either. I still get the array of "game" but nothing else. The above viewmodel still seems wrong to me... The "result" should be attached to the game entity (vm.game) and not the vm - currently this will give vm.result and there is a result per game (so vm.game.result), not per array of games. This is why I'm wondering if I need to be extending the entity via breeze. I could do this in normal javascript but it seems that breeze or knockout should be able to do this far easier?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload