0

I am slightly confused by this, as i am sure that all variables are taken to the 'top' of the javascript before run time and then processed from there.

So my error

 TypeError: hutber.portfolio.ko is undefined
 [Break On This Error]  
 items: ko.observableArray(hutber.portfolio.ko.data),

Object

(function ($) {
"use strict"; //For good development standards :)
hutber.portfolio = {
    init: function(){
        e(typeof(hutber));
        hutber.portfolio.changeOptionsBoxHeight();
        //Bind the height resize in window resize
        $(window).resize(function(){
            hutber.portfolio.changeOptionsBoxHeight();
        });
        //start KO
        hutber.portfolio.ko.init();
    },
    changeOptionsBoxHeight: function(){
        var height = $(window).height();
        $('.portfolio--options').height(height-400);
    }
};
hutber.portfolio.ko = {
    init: function(){
        ko.applyBindings(new hutber.portfolio.ko.portfolioViewModel());
    },
    data: [],
    items: ko.observableArray(hutber.portfolio.ko.data),
    portfolioViewModel: function(){

        hutber.portfolio.ko.items = ko.observableArray(hutber.portfolio.ko.data);

        $.getJSON('/js/pages/portfolio.json').done(function(info){
            hutber.portfolio.ko.data = info;
            hutber.portfolio.ko.items (hutber.portfolio.ko.data);
        });
    }
};
hutber.portfolio.init();
})(jQuery);

I really wanted to upload this to a fiddle but for some reason, i'm getting js errors on their site. I believe my firewall is blocking certain files from loading.

3
  • I'm getting a hutber is not defined. You can use this jsbin instead of jsfiddle Commented Jan 11, 2013 at 14:05
  • 1
    @PabloGonzálezAlba: hutber has not been defined. But that is not the question. Commented Jan 11, 2013 at 14:07
  • cheer for that :) add hutber = {}; above (function ($) { to declare it Commented Jan 11, 2013 at 14:08

2 Answers 2

1

At the point ko.observableArray(hutber.portfolio.ko.data) is run, hutber.portfolio.ko has not be defined yet.

You can work around it like that:

hutber.portfolio.ko = {
    init: function(){
        ko.applyBindings(new hutber.portfolio.ko.portfolioViewModel());
    },
    data: [],
    portfolioViewModel: function(){

        hutber.portfolio.ko.items = ko.observableArray(hutber.portfolio.ko.data);

        $.getJSON('/js/pages/portfolio.json').done(function(info){
            hutber.portfolio.ko.data = info;
            hutber.portfolio.ko.items (hutber.portfolio.ko.data);
        });
    }
};

hutber.portfolio.ko.items = ko.observableArray(hutber.portfolio.ko.data);

But at this point hutber.portfolio.ko.data is always []. So you might as well put ko.observableArray([]) in your original code.

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

3 Comments

does indeed fix the error, another one pops up. but thats really the point now is it :) I hadn't thought about adding to the object on the fly like that. Not sure why i was so dead set on making sure it was defined in the objects chain as it were.
Also hutber.portfolio.ko.items (hutber.portfolio.ko.data); this row might throw an error as long as ko.observableArray() does not return a function. But I do not know that much about Knockout.
ye, i also thought that it had to be a function. Enough hunting round turns out it doesn't need to be a function. Just an object, array etc etc
0

Just guess: Because you accessing variable before you declare it?

1 Comment

pretty sure i have declared it. :/

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.