DAO pattern in Knockout.js

73 views
Skip to first unread message

Wet Feet

unread,
Aug 28, 2017, 12:00:35 AM8/28/17
to KnockoutJS
Has anyone implemented the DAO pattern for serializing and deserializing data from/to a database?

In DAO, the data access object is usually a simple Plain Old JavaScript Object (POJO) that describes the business data model.

I see 2 different methods how Knockout.js can be used with DAO. I am not sure which style is correct.

Method 1: Use ko.observable and ko.observableArray in DAO

With Knockout.js, should there be ko.observable or ko.observableArray fields inside a DAO object?

function DaoObject(){
 
this.field1 = ko.observable('value'); // ko.observable field in data access object?
 
this.field2 = ko.observable('value');
}

function ViewModel(){
 
var self = this;

 
self.field1 = ko.observable();
 
self.field2 = ko.observable();

 
self.update = function(){
   
var dao = new DaoObject(); // Returned from somewhere like a database

   
self.field1 = dao.field1; // replace the original ko.observable with a new ko.observable in DaoObject, rather than update value.
   
self.field2 = dao.field2; // replace the original ko.observable with a new ko.observable in DaoObject, rather than update value.
 
}
}





Method 2: Keep DAO as simple JavaScript object, and update the ViewModel's value field by field

Or should DAO object remains a simple JavaScript object, and then we manually copy over and update the value over?

function DaoObject(){
 
this.field1 = 'value';
 
this.field2 = 'value';

}

function ViewModel(){
 
var self = this;

 
self.field1 = ko.observable();
 
self.field2 = ko.observable();

 
self.update = function(){
   
var pojo = new DaoObject(); // Returned from somewhere like a database

   
self.field1(pojo.field1);
   
self.field2(pojo.field2);
 
}
}



Reply all
Reply to author
Forward
0 new messages