0

So I have four models:

  • Lobby (the view model)
  • Messages
  • Campaigns
  • Campaign

And I have them set up like so:

       Lobby: function(Messages, Campaigns) {
            var self = this;
            self.chat = new Messages();
            self.campaigns = new Campaigns();
        },
        Campaigns: function() {
            var self = this;
            self.campaigns = ko.observableArray();
        },
        Messages: function() {
            var self = this;
            self.message = ko.observable("");
            self.messages = ko.observableArray();
        },
        Campaign: function(campaign) {
            var self = this;
            var status = ["In Lobby", "In Game", "Finished"];
            self.Id = ko.observable(campaign.Id);
            self.url = ko.observable("/matchmaking/" + campaign.Id);
            self.mapName = ko.observable(campaign.Map);
            self.mapImage = ko.observable("/Images/"+ campaign.Map +".jpg");
            self.notes = ko.observable(campaign.Notes);
            self.status = ko.observable(status[campaign.status]);
        }

And I have a simple binding to view the each message in the chat model:

   <div id="chat-messages" data-bind="foreach:chat.messages" style=" max-height: 250px; min-height: 80px; overflow-y: auto;">
                        <div data-bind="text:$data"></div>
                    </div>

Apply bindings is added:

  ko.applyBindings(this.viewModel); //where this.viewModel is an instance of Lobby

However I get the following error:

Uncaught ReferenceError: Unable to process binding "foreach: function (){return chat.messages }" Message: chat is not defined

I've tried putting them all in one model, splitting them out like this. Al sorts, it just doesn't want to work. Any suggestions?

3
  • 2
    foreach:chat().messages? Commented Feb 24, 2016 at 13:46
  • @artm is probably spot on, kindof a typo I'd guess, happens to the best of us :-) (@QBM5 chat is an observable on Lobby) Commented Feb 24, 2016 at 14:30
  • Yes artm, thank you, post as answer and I will accept Commented Feb 24, 2016 at 14:33

2 Answers 2

1

Lobby's chat is a function of Messages type so using the binding data-bind="foreach:chat.messages" translates to Lobby.chat which returns a function. So you need to change your binding to data-bind="foreach:chat().messages"

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

Comments

1

your problem is that chat is an observable and as such you should call chat().messages :)

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.