General question about variable scope

I am facing a problem with variable scope…I do not want to complicate things,so for now I am going to be very general in my description since I am dealing here with a problem which relates to backbone.
Suppose I have 2 functions…function a and function b.

Function a is called first and function b is called secondly.
Ιn function b there is a value assignment in a global variable—this same variable is located inside function a

It would be reasonable to assume that once execution of function b ends…the value that was set there in the global variable will persist-in function a.

Meaning the global variable in function b will have the value from the assignment in function a.

Am I right or wrong?I hope I was clear…else I will restate the issue.

It depends upon how the variable is scoped.

Outside of a function, if you use var varName = "this"; then it will be globally available to all functions. If you use varName = "this"; (without var), then it will be available only outside of functions.

INSIDE a function, if you use var varName = "this";, it will be available only to that function. If you use varName = "this"; (without var), it will be globally available outside the function.

Can’t really say without seeing code.

V/r,

:slight_smile:

here is the code(jsbin)

  1. First,the render function is called at line 34
  2. Then…the parse function is called at line 9 which assigns a value at the global variable closed at line 10

My problem is that the value assigned above is not set at line 62…(the same global variable as mentioned above)

I miss something here but I cannot find what
Maybe…**maybe…**in order for the variable assignment to take place in the render function…this render function must be called again…for the second time…after parse.

Unfortunately, where I work jsbin is blocked. jsfiddle I can access, but it has it’s issues, too.

I used to be able to access Slexy with no problems. That is now no longer the case. :frowning:

V/r,

:slight_smile:

do you want me to make a (js)fiddle?

Unfortunately, it’s currently the only way I can view code bins. Unless you want to paste it all, here.

:slight_smile:

Just skimming over the code and this caught my eye:

var Events = Backbone.Collection.extend({
    model: Event,
    url: 'events',
    parse: function (response) {
      closed = response[0].closed_days;
      package = 'test';
      servstatus = response.servstatus || response;
      staffstatus = response.stafstatus || response;
      appointments = response;
      return response;
    }
});

As it stands, your parse method isn’t modifying the response at all, it’s just passing it through unchanged (as the default method does). It seems like what you actually want to be doing in your parse method is this:

parse: function (response) {
  return {
    closed: response[0].closed_days,
    package: 'test',
    servstatus: response.servstatus || response,
    staffstatus: response.stafstatus || response,
    appointments: response
  };
}

I tried the above but my appointments not even show in the calendar anymore…probably in order for this code to work I must make some refactoring,
Let us focus on the issue at hand,

 var Events = Backbone.Collection.extend({
    model: Event,
    url: 'events',
     parse: function (response) {
        closed=response[0].closed_days;
        package = 'test';
        servstatus = response.servstatus || response;
        staffstatus = response.stafstatus || response;
        appointments = response;
        return response;
        
    }
});`

 var EventsView = Backbone.View.extend({//σχετικό με το calendar view
  closed:this.closed,
    initialize: function () {
        _.bindAll.apply(_, [this].concat(_.methods(this)));
     },
    render: function () {
  console.log('clos',window.clos);
        this.$el.fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,basicWeek,basicDay'
            },
            selectable: true,
            selectHelper: true,
            hiddenDays:this.closed,//here is the problm
            editable: true,
            ignoreTimezone: false,
            select: this.select,
            eventClick: this.eventClick,
            eventDrop: this.eventDropOrResize,
            eventResize: this.eventDropOrResize
        });
    },

I managed(with some tricks) to call render after parse is called,I cannot understand though whyhiddenDays()link does not take the value of this.closed

var EventsView = Backbone.View.extend({//σχετικό με το calendar view
  closed:this.closed,

Here the value of this is the window object, and the window object already has a read-only property called closed: https://developer.mozilla.org/en-US/docs/Web/API/Window/closed

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.