IT:AD:Durandal.JS:HowTo:Create A Module

The module has the following characteristics:

  • it is a straight js object (ie, it does not inherit from any Durandal base viewmodel class.
  • but as all Durandal code is written as IT:AD:Require.JS AMD files:
    • it starts off with the define method, and lists dependencies (relative to your app's base directory, where main.js is), and then invokes your function, with the name you want to refer to it as:

//Start the model listing the requirements that this model needs
define([ 'durandal/system', 'durandal/plugins/router', 'somecustomfolder/somecustommodule'],
    function(system, theRouter, myZingZang) {
       //from withih, one can use 'theRouter' to refer to the object that came in from `durandal/plugins/router` (ie Sammy.js)
       //or use myZingZang to refer to your other module's object -- the names don't have to math 1-1. 
       //but they do have have to match order 1-1... (a:A, b:bb,c:whatever)
    }

  • Important: the return value of the function is what will be imported by other modules when they call require.
  • The return value can be either an object (a singleton, shared throughout app) or function (unique per module load).
  • All modules have an id (more on that later).

An example of a singleton non-visual module would be:

//a repo
define(['durandal/system'],
    function(system){
      //create a singleton model object with methods and return the object:
      return {
         getCustomer = function(id) {...do some ajax or better yet, use breeze.js...}
         getCustomers = function(name) {...do some ajax or better yet, use breeze.js...}
      }
    });

or create a unique object per require:

//a repo
define(['durandal/system'],
    function(system){
      //create a class def:
      var repo = function() {}
      //Add some methods to the class def:      
      repo.prototype.getCustomer =function(id){...}
      repo.prototype.getCustomers = function (name) {...}
      
      //return the class def (not a new object created from the class def):
      return repo;
    });

  • /home/skysigal/public_html/data/pages/it/ad/durandal.js/howto/create_a_module.txt
  • Last modified: 2023/11/04 01:42
  • by 127.0.0.1