Jump to content

conditional within an object


jimfog

Recommended Posts

Take a look at this fiddle https://jsfiddle.net/fiddlehunt/andwu6Lf/ from lines 2 to 10...in tha JS pane.

 

You see an object which contains two objects.

 

I want the second object(e-mail) to be there if a specific condition is met-depending the value of a variable.

 

So..can a conditional be placed inside an object.

The result must be another object or none.

Link to comment
Share on other sites

there is another slight problem...I do not want to create a separate topic about it.

This condition must contain the presence of global variable.

 

Take a look at this http://jsbin.com/silajo/74/ at line 25...the variable is package...and it is produced by parse function at line 9(all of this of course is Backbone code)

at a backbone collection.

 

The problem is that I get a message package is not defined....which means I cannot make package available to the User model.

 

How I could overcome this?

Link to comment
Share on other sites

It's not a great idea to use globals in object definitions like that. I'm not familiar with backbone enough to verify this, but I suspect that the code which defines that variable is not executed before the code that uses it.

Link to comment
Share on other sites

It's not a great idea to use globals in object definitions like that. I'm not familiar with backbone enough to verify this, but I suspect that the code which defines that variable is not executed before the code that uses it.

What is the alternative to globals in object definitions?

 

What you suspect is wrong...the code which defines the variable is directly above from the code that uses it.

This code is in lines 6-16...in this jsbin http://jsbin.com/silajo/74/ ....(and is being used in line 25)I do not understand where is the problem....

 

Anyway I asked the question cause I think we are dealing here with javascript principles and not principles specific to backbone.

Link to comment
Share on other sites

What is the alternative to globals in object definitions?

Passing values to constructors.

What you suspect is wrong...the code which defines the variable is directly above from the code that uses it.

That's not what I see. What I see is that you're creating something called Events, apparently by extending Backbone.Collection, and you send it an object with a model property, a url property, and a parse method. Then, right after that, I see you creating an object which uses a variable called package, and then you send that object to another extend method. What I do not see is where you execute the Events.parse method to define the package variable. Creating the parse method does not execute the code and define those variables. Those variables get defined when you execute parse. So, I'm not wrong, unless Backbone will implicitly call that parse method when you extend the object, but if it did that then the variable would be defined when you try to use it. Apparently it's not defined, so apparently Backbone does not execute that parse method automatically.
Link to comment
Share on other sites

Passing values to constructors.That's not what I see. What I see is that you're creating something called Events, apparently by extending Backbone.Collection, and you send it an object with a model property, a url property, and a parse method. Then, right after that, I see you creating an object which uses a variable called package, and then you send that object to another extend method. What I do not see is where you execute the Events.parse method to define the package variable. Creating the parse method does not execute the code and define those variables. Those variables get defined when you execute parse. So, I'm not wrong, unless Backbone will implicitly call that parse method when you extend the object, but if it did that then the variable would be defined when you try to use it. Apparently it's not defined, so apparently Backbone does not execute that parse method automatically.

It seems you are right....I created a global variable for testing above the code where it is supposed to be used and it worked without problems.

 

So the problem now is how to execute parse...if you go at line 181 at the jsbin you will see

 var events = new Events();

maybe the above code nust go above the model where package is to be used...I am not sure...I am just thinking.

Link to comment
Share on other sites

I don't know, I don't know if something in Backbone will call that parse method automatically or if you have to do it explicitly. A better question might be why you're trying to set a bunch of global variables inside a method that you're not sure when it gets called, but you have other code relying on those globals.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...