0

Ok after a day I managed to narrow down the problem to 2 lines of code. Maybe I am trying to use the this statement incorrectly.

        function scheduleItemView(myId){
            this.update = function(show){
                document.getElementById(this.id+'-title').innerHTML = show.title +": "+ show.startDate;
                document.getElementById(this.id+'-title-overlay').innerHTML = show.title +": "+ show.startDate;
                document.getElementById(this.id+'-description').innerHTML = truncate(show.description,190);
                document.getElementById(this.id+'-time-start').innerHTML = show.startTime;
                document.getElementById(this.id+'-time-end').innerHTML = show.endTime;
            };
            this.id=myId;
            return true;
        }

        function nowNextView(){
            this.now = new scheduleItemView('now');
            this.next = new scheduleItemView('next');
            this.update = function(type,args){
                var myshow=args[0];

    // problem is below. I have to use the global name to access the update method.   
                    myNowNextView.now.update(myshow.now);
                    myNowNextView.next.update(myshow.next);

    // whereas what I want to do is reference them using the "this" command like below.
    //  this.now.update(myshow.now);
    //  this.next.update(myshow.next);
    // the above doesnt work. The update method in scheduleItemView is not seen unless referenced globally
    // BUT even more infuriating, this.now.id does return "now" so it can access the object, just not the method
    // any ideas?

            };
         }

object is then instantiated with

var myNowNextView = new nowNextView();

and then I run the method:

myNowNextView.update(stuff);

I tried to describe the problem within the body of the program. No error in the code was thrown, and I had to do a try/catch before it grudgingly told me that it couldn't find the method. Is the design flawed somehow? can I not do this? Many thanks in advance, Steve

Blender
  • 289,723
  • 53
  • 439
  • 496
  • Actually it looks like it should work. If you call `myNowNextView.update()`, then `this` inside `update` will refer to `myNowNextView`. – Felix Kling Mar 09 '11 at 21:29

2 Answers2

0

I think you could really benefit from studying closures a bit in javascript. It seems like you are trying to apply a traditional OO approach to js objects, and that won't give you the results you are expecting.

I would recommend reading over this post for an easy way to use closures: http://howtonode.org/why-use-closure

Something like this:

<html>
<head>
<script type="text/javascript"> 

  function createScheduleItemView(myId){

    var _show = false

    return {
        setShow : function setShow(show) {
            _show = show
        }, 
        update : function update(){
            document.getElementById( myId +'-title').innerHTML = _show.title;

        }
      }

   }

   function createNowNextView(){

       var _now  = createScheduleItemView('now');
       var _next = createScheduleItemView('next');

       return { 
            publicVar : "Example",
            update : function update(args) {
                var myshow=args[0];
                _now.setShow(myshow.now)
                _now.update();
                _next.setShow(myshow.next)
                _next.update();
            }

        };
     }

   function runIt() {
        nowNextView = createNowNextView()
        args = []
        showArgs = {
            now : {title : "Beauty and the Beast"},
            next : {title: "I did it myyyyyy way"}
        }
        args.push(showArgs)
        nowNextView.update(args)

        //Private variables can not be accessed
        //console.log(nowNextView._now)
        //undefined
        //
        //But anything you return in the object is public
        //console.log(nowNextView.publicVar)
        //Example

   }


</script>
</head>
<body onload="runIt()">

<h3>Now Showing:</h3>
<p id="now-title"></p>

<h3>Up Next:</h3>
<p id="next-title"></p>

 </body>
 </html>
Tom Gruner
  • 9,635
  • 1
  • 20
  • 26
  • Also for a complete discussion you can see: http://howtonode.org/what-is-this and http://stackoverflow.com/questions/1595611/how-to-properly-create-a-custom-object-in-javascript – Tom Gruner Mar 09 '11 at 21:50
0
function scheduleItemView(myId){
this.update = function(show){
  document.getElementById(this.id+'-title').innerHTML = show.title +": "+ show.startDate;
  document.getElementById(this.id+'-title-overlay').innerHTML = show.title +": "+ show.startDate;
  document.getElementById(this.id+'-description').innerHTML = truncate(show.description,190);
  document.getElementById(this.id+'-time-start').innerHTML = show.startTime;
  document.getElementById(this.id+'-time-end').innerHTML = show.endTime;
};
this.id=myId;
}

function nowNextView(){
var myshow=args[0];
var scope = this;
this.now = new scheduleItemView('now');
this.next = new scheduleItemView('next');
this.update = function(type,args){
      scope.now.update(myshow.now);
      scope.next.update(myshow.next);
};
}
Gergely Fehérvári
  • 7,811
  • 6
  • 47
  • 74