0

This should be a pretty trivial question, but it's getting me crazy! I't like step 1 of the tutorial, but I can't it to work.... so this is my code:

/* Model definition */
    var PrivateSalesMenuModel = function () {
    var self = this;
    this.privateSales = ko.observableArray();
    this.addPrivateSale = function (privateSale) {
        var newPs = new PrivateSaleMenuItem(privateSale);
        self.privateSales.push(newPs);
    };
};

var PrivateSaleMenuItem = function (ps) {
    this.title = ps.Description;
    this.hasActual = ps.HasActual;
    this.hasSale = ps.HasSale;
    this.listaId = ps.ListaId;
    this.isSelected = false;
};
/* end model definition*/

var privateSalesMenuModel = new PrivateSalesMenuModel();

ko.applyBindings(privateSalesMenuModel);

Pretty simple... I have an object that represent my model, that is a collection of others objects, called PrivateSaleMenuItem. Problem is that addPrivateSale didn't work as expected. Somewhere in the code I do

privateSalesMenuModel.addPrivateSale(ps);

where ps is an object created by other JavaScript functions... anyway is exactaly the object I need in the constructor of PrivateSaleMenuItem, so it's consistency is not the problem. The problem seems o be that self.privateSales.push(newPs); doesn't work... after that inocation, the number of privateSalesMenuModel.privateSales is still 0. Why is that?

Edited

I put toghether an example in jsFiddle with this same exact code, and it works fine, so I suspect something in my page make the push method of observableArray stop working... how can I find out what it is?

ops... the link of jsfiddle http://jsfiddle.net/YBHf5/

8
  • do you get any console errors? Commented Dec 16, 2013 at 11:43
  • ko.observableArray() is defined?... the function return a correct array? the method is accessible? Commented Dec 16, 2013 at 11:48
  • @Tanner: nope, everything's clean. Commented Dec 16, 2013 at 11:51
  • @Frogmouth: I thought that it shuold throw some exceptions if the function wouldn't be accessible or stuff like that? Am I wrong? How can I answer to your question properly? I saw that debugging, I get into the knockuotjs code that implement the push... so I think the method call is valid... Commented Dec 16, 2013 at 11:56
  • check the object that you get from that other function and pass to the constructor. There might be some error like typo in property name when you are assigning the values. Commented Dec 16, 2013 at 12:01

2 Answers 2

1

It looks like you are mixing this and self in your code here:

    var PrivateSalesMenuModel = function () {
    var self = this;
    this.privateSales = ko.observableArray();
    this.addPrivateSale = function (privateSale) {
        var newPs = new PrivateSaleMenuItem(privateSale);
        self.privateSales.push(newPs);
    };
};

Changing them to all use self fixes it like this:

var PrivateSalesMenuModel = function () {
    var self = this;
    self.privateSales = ko.observableArray();
    self.addPrivateSale = function (privateSale) {
        var newPs = new PrivateSaleMenuItem(privateSale);
        self.privateSales.push(newPs);
    };
};

Please see working fiddle here:

http://jsfiddle.net/YBHf5/1/

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

1 Comment

Long shot but do you have an external link I can view it on?
0

Problem solved.... it was a trivial problem indeeed, with a trivial solution: I just needed to push the external script declaration at the end of the page. I had the script (in which the code provided on the question is present) at the top of the page, inside the body, but above anything else. Just putting the script reference at the end of the body and everything works as expected again. Sorry for wasting your time... but this little issue is very hard to find out, 'cause the reason is not clear at all while debugging

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.