Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Welcome, Guest. Please login or register.
December 18, 2017, 05:49:13 AM
Home Help Search Login Register
News: Please DM @puremvc on Twitter to gain forum access. Spambots are why we can't have nice things.
Pages: [1]
Print
Author Topic: puremvc with requirejs and or nodejs.  (Read 7265 times)
drr00t
Newbie
*
Posts: 5


View Profile Email
« on: June 21, 2012, 08:43:16 PM »

Hi,

I have use Backbone with requirejs to load templates, i was news native port for greate puremvc architecture, and i trying replace Backbone.

I try load with requirejs but i couldn't. There is any way to add something like that:

Code:
if (typeof module !== 'undefined' && module.exports) {
             module.exports = puremvc;
         } else if (typeof define === 'function' && define.amd) {
             define(function(){return puremvc;});
         }

I think that define from requirejs is coliding with define from puremvc.

Any sugestion?

thank you guys.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW
« Reply #1 on: June 22, 2012, 07:06:13 AM »

I don't personally know about those frameworks, but I can say that puremvc.define should not collide with anyone else's define method since it is namespaced as puremvc.define() rather than just define().
Logged
drr00t
Newbie
*
Posts: 5


View Profile Email
« Reply #2 on: June 22, 2012, 11:20:04 AM »

Hi,

Thanks for answer,

http://requirejs.org/ is just a loader, i like to user for load external templates html and use with any javascript template engine like: doT, jquery template, etc... Normally requirejs need tha the library export their symbols, like i did on post before. I try do this but not work, because requirejs and puremvc both has a define method and i was making a change inside puremvc namespace, it generate the problem.

1 - But a try load diferente way, i just declare a requirejs module in file puremvc-wrap.js, like this:
Code:

define([lib/puremvc/puremvc-1.0.1],function(){
      return window.puremvc;
});


2 - And to use new puremvc module in main.js file, i did this:
Code:
require.config({
    puremvc:'libs/puremvc/puremvc-wrap'
});

3 - To use new puremvc module declaring my application facade in applicationFacade.js file, i did this:
Code:

define(['puremvc'],function(puremvc){
      return puremvc.define(
            {
                    name: demo.ApplicationFacade
                    parent: puremvc.Facade
             },
            {
                    startup:function(){
                        if(!this.initialized)
                              this.initialized = true;
                    }
             },
            {
                    getInstance:function(multitonKey){
                         var instanceMap = puremvc.Facade.instanceMap;
                         instance = instanceMap[multitonKey];
                           
                          if(instance)
                                 return instance;
                           return instanceMap[multitonKey] = new demo.ApplicationFacade(multitonKey);

                    },
                    NAME:'demo'
             }

      );
});


5 - To use declared facade i back to main.js file, and i did this:
Code:

define(['applicationFacade'],function(demo){
      demo.ApplicationFacade.getInstance(demo.ApplicationFacade.NAME).startup();
});

All work nicely, thank you for answer, sorry for the misundertanding. keep up with greate work.

Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW
« Reply #3 on: June 23, 2012, 04:08:03 PM »

Thanks for posting your final approach, I'm sure this will be useful to others, so I think I'll make this topic sticky.

Cheers,
-=Cliff>
Logged
mikebritton
Full Member
***
Posts: 42


View Profile Email
« Reply #4 on: September 21, 2012, 10:26:04 AM »

Indeed, very helpful.  Props all around.
Logged
jrmorrill
Newbie
*
Posts: 6


View Profile
« Reply #5 on: November 10, 2012, 07:10:09 PM »

Since this was created back in June, I think the syntax was using the 1.0 API. Here's some updates for how to add puremvc as a dependency if you are using the requireJS 2.0 API:

If you have the following directory structure:


-+
  |- index.html
  +- js/
  |   +- app/
  |   |   |- controller
  |   |   |- model
  |   |   |- view
  |   |   |- ApplicationFacade.js
  |   |
  |   +- lib/
  |   |   |- require.js
  |   |   |- puremvc.js
  |   |
  |   |- app.js
     




Put the following script tag in your index.html

index.html
Code:
<script data-main="js/app.js" src="js/lib/require.js" type="text/javascript"></script>

js/app.js
Code:

requirejs.config({
// By default load any module IDs from js/lib
baseUrl: 'js/lib',
// except, if the module ID starts with "app",
// load it from the js/app directory. paths
// config is relative to the baseUrl, and
// never includes a ".js" extension since
// the paths config could be for a directory.
paths: {
app: '../app'
},
// or if the module begins with "puremvc"
// we will use the global "puremvc" variable
// as the location of the module
shim: {
'puremvc': {
deps: [],
exports: 'puremvc'
}
}
});

// Start the main app logic.
requirejs(
['app/ApplicationFacade'],
function(ApplicationFacade)
{
var app = ApplicationFacade.getInstance(ApplicationFacade.NAME);
app.startup();
}
);


The js/app/ApplicationFacade.js file remains unchanged. Notice that the only real difference is that the shim is now built-in to requireJS. There is no need to wrap prototype as a module since we can setup support for it in the config. Just a simpler way to include other libraries that don't conform to the requireJS pattern for defining modules.

Also, on a side note, these "shim" modules can also have dependencies that they require loaded with the
Code:
deps: [],
property. More info can be found here:

http://requirejs.org/docs/api.html#config-shim

Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW
« Reply #6 on: September 11, 2014, 08:18:59 AM »

Excellent! Thanks for the update.
Logged
Pages: [1]
Print
Jump to: