2
votes

I am new to requirejs and AMD module functions, and I am trying to pass a parameter from a require call, but I am running into some troubles. I am trying to pass the obj in a require call to the fun method. This require call is within a define method as shown in the code below. I following the instructions from this blog: http://blog.novanet.no/4-strategies-for-passing-parameters-to-requirejs-modules/

define(['require'],function(require){
    var obj= {name:'random'};
    require({
        config : {
           'tagging' : obj
        }
    },['fun'],function(promise){
        promise.then(function(ret){
        //do something.
      });
   }   
}

Then I want to use the passed obj parameter in this below code. In fun.js

//Passing obj to fun.js

define(['module','lie'],function(data,Promise){
    var promise = new Promise(function(res,rej){
        //do something
    });
}

But instead I am getting the following error on the console:

TypeError: depMaps.slice is not a function

Any clues on fixing this?

PS:Also if there are any better methods, kindly let me know.

Thanks,

1
What are you trying to do by supplying { config : { 'tagging' : obj } } to require call - Vishwanath
I want to pass the 'obj' to fun.js(and use it as data). There I will do some computation. This 'obj' is something that I am calculating inside the calling function. - zelta
I am using this link for referance, sorry for not providing it inside the question. link: blog.novanet.no/… - zelta
Can you please edit your question and make it more clear by adding what exactly you want to happen... where do u want to use your data. Add that in the code... - Vishwanath
I hope the clarifies the question, and apologies for sounding that naive. :) - zelta

1 Answers

0
votes

This can be done easily by returning a function which returns the promise rather than returning the promise directly. Then you can pass the data to this function for getting the promise and do whatever calculations that you want to do with the promise and data.

define(['require'],function(require){
    var obj= {name:'random'};
    var data = {
       'tagging' : obj
    }
    require(['fun'],function(promiseGetter){
      promiseGetter(data).then(function(ret){
        //do something.
      });
   }   
}

and fun.js

define(['lie'],function(Promise){
    var promise = new Promise(function(res,rej){
        //do something
    });
    return function(data){ 
         //DO whatever you want to do with data here
         return promise;
    }
}