Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Dealing with subclasses

$
0
0
Hi,

//I have a model class called 
Portal.Document = can.Model({},{});

//and a couple of subclass
Portal.WordDocument = Portal.Document({}, {});
Portal.ExcelDocument = Portal.Document({}, {});

// In my app I render a table of Portal.Documents (I fetch the model instances using findAll)
// This call will include documents of all types in one list, e.g. word and excel
Portal.Document.findAll()
.done(
      function(){ 
      renderDocuments(); 
});

// Here is where it gets tricky: As a user I want to now edit one document, e.g. if I wanted to edit the individual metadata (in a single page //application scenario). As it is a Word document I want to "cast" the generic Portal.Document instance as a Portal.WordDocument, in order //to make the subsequent code cleaner (more object oriented perhaps).
//At the moment I do this:
var wordDoc = new Portal.WordDocument(doc.serialize());

//However One of the main problems with this is that there are now two model instances representating the same object ( same row 
//in the DB table essentially). So when I update the attributes of one, the canJS magic does not automatically update the other.

Am I going about this the right way? I'm trying to avoid having a Portal.Document model class that has code that needs to handle all types of documents, e.g.
//I want to avoid this
if(word){
      do this
}else if(excel){
      do this
}

Am I going about this the right way?

To summarise, I want a generic findAll for documents, but would like to separate unique behaviour for different types into subclasses. But I also want the live binding to continue working, so that when the subclass is updated, so is the instance of the "generic" document.

I'm still on CanJS 1.1 unfortunately.

Thanks in advance,
Bryon

Viewing all articles
Browse latest Browse all 3491

Trending Articles