1

Supposed that I created a js object like this

Cart = function Cart(){
    this.contents = new Array();
}

Cart.prototype = {
   add : function(obj){
      this.contents.push(obj);
   }
}

and put it in the project lib folder so that this object can be used project-wide.

Is it possible to use this object persistently in the template backend (js file)? For example, I declared :

Template.pageA.rendered = function(){
    var smallCart = new Cart();
}

Can I use it in the template event? For example:

Template.pageA.events = {
  'click button#add' : function(event){
     smallCart.add('newItem'); //Is this object as same as the one in rendered?
   }
} 

I have been doing stuff by using Sessions but when there are a lot of operations to be done, the events will be cluttered with business logics and calculations. I want to avoid this, thus I am thinking of putting the logics into Javascript object functions.

Will this approach work? Will the object stay persistent?

3 Answers 3

1

You can place that code inside a Meteor.startup, so the object will persist always.

Meteor.startup(function(){
 Cart = function Cart(){
    this.contents = new Array();
 }

 Cart.prototype = {
   add : function(obj){
      this.contents.push(obj);
   }
 }
})

Is this object as same as the one in rendered? yes

Sign up to request clarification or add additional context in comments.

1 Comment

So this should be done on the client side right? And if there are multiple connections to the server, I supposed each of the machine won't share the same object. Am I right?
1

From my point of view, putting the class definition of your cart into the lib folder can be good. If it's client-side code only, you can store it into a cart.js file into your client folder.

This definition will be available on client.

Then you need to store an instance of your cart. Instanciate a cart into your template can be a good idea (you don't need it before that), but your need to store a reference into you client side application scope (usually into app.js at the root of your client folder for example)

client/cart.js

Cart = function() {}....

client/app.js

myCart = null;

Into your template, you can instantiate the cart

myCart = new Cart();

If you're using your only on one template, you can store the instance directly to the template into the controller logic. Attach it to the rendered of the template is a bit wired, because you usually handle template rendering logic.

This way, a cart is going to be created for every user which come on your site and called the template. If you have a multi page app using iron router, myCart will be available whatever the page as soon as you have already create it into your template.

Be aware, in this way, if the client "refresh" the page reloading the JavaScript code, you'll loose the current cart. You need to persist the cart on local storage in order to support the refresh. Not worth in all the case, but for a shopping cart, can be usefull

Comments

0

Putting the Cart code in lib is correct, it will be available everywhere.

As far as smallCart is concerned you have declared it inside a function so it isn't available to the other template function.

Declare it outside the two functions and it will be available to both.

var smallCart;

Template.pageA.rendered = function(){
    smallCart = new Cart();
}

Template.pageA.events = {
  'click button#add' : function(event){
     smallCart.add('newItem'); //Is this object as same as the one in rendered?
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.