I am using Konckout, Breeze JS, Durandal JS, ASP.NET Web API but my question is specific to Breeze and Knockout.
How do I automate extending Knockout observables from entities returned by Breeze query?
I have a list of Customers being returned via Breeze and they have DB specific fields which I would like to make presentable. In particular I want:
- UTC timestamps (e.g. "2011-07-02 13:20:13.8061582 +00:00") to show as local date & time (e.g. "July 2, 2011 1:20 PM")
- Decimal amounts (e.g. "2500.12") to be formatted as currency (e.g. "$2500.12")
- Add specific css class based on status (e.g. is Status="Available", add cssStatus="item-available")
From what I have read (please let me know if I am wrong), the way to do this is not to use custom bindings anymore but to extend KO observables using .extend()
function.
My dataservice.js has following Breeze query that accepts observable and sets it to data results.
// -- snip --
function getCustomers(koCustomers) {
var query = breeze.EntityQuery
.from('Customers');
return manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
function querySucceeded(data) {
koCustomers([]);
koCustomers(data.results);
}
}
// -- snip --
For reference here is my viewmdel as well
define(['durandal/app',
'durandal/system',
'durandal/plugins/router',
'services/config',
'services/logger',
'services/datacontext'],
function (app, system, router, config, logger, datacontext) {
'use strict';
var customers = ko.observableArray();
var viewModel = {
title: 'Vehicles',
activate: activate,
customers: customers
};
return viewModel;
function activate() {
return datacontext.getCustomers(customers);
}
});
I am thinking I need some sort of mapping function that would accept observable and data.results and then loop through each result and expand it... Any example would be appreciated...