javascript - How to define a "type" in AMD/RequireJS style -
i'm working on durandal spa, , i've setup views , viewmodels. i'm unclear how should creating "model" objects on client side. instance, let's want define , create "person" model object.
everything else in app defined amd style. allows durandal/requirejs automatically load files needs.
so, first thought might define "person" model type in models/person.js file, this:
define(function () { var id = ko.observable(0); var firstname = ko.observable(''); var lastname = ko.observable(''); return = { id: id, firstname: firstname, lastname: lastname, }; });
however, don't think right, because here don't think there's way create individual "person" objects. instance, let's wanted create 2 "person" objects. durandal/requirejs happily load in "person" module, it's 1 object--and may want create instance of "person" type, or 5 more instances, etc.
what's appropriate way this? helping clear confusion!
update: i've gotten responses explain ways this. think confusion stemmed fact "module" not "type", or "class". it's more singleton object. if want define type, , put inside module, need create module is, essentially, factory.
is right?
a factory makes sense in case. but, have clarifying question: factory accepted method defining model types in durandal/requirejs/amd style javascript app? i'm sure i'm not first person run issue. i'm wondering how has been solved before, , (especially) if there accepted convention in amd world on how define reusable types.
define(function () { return function(){ var self = this; self.id = ko.observable(0); self.firstname = ko.observable(''); self.lastname = ko.observable(''); }; }); define(['models/person'], function(person){ var myperson = new person(); });
Comments
Post a Comment