1

I have the following view model. Upon logging in I want the user to have a select box listing all the cities in the database. Here's the view model

function ppfoViewModel(){
        var self= this;
        self.userName = ko.observable();
        self.password = ko.observable();
        self.loggedIn = ko.observable(false);
        self.validateUser = function(){
            if(self.userName()=='Admin'&&self.password()=='Admin'){ 
                var temp;
                self.loggedIn(true);
                $.get("dbhandler.php", { "fun": "cities"}, function (data) {self.cities(data);});               
            }else{
                self.loggedIn(false);
            }};
        self.cities = ko.observableArray()
        self.selectedCity = ko.observable();
        self.store = ko.observable();
        self.drink = ko.observable();
        self.test = ko.observable();
    };

The ajax request calls a page that returns the following string:

[{"cityName":"Provo","cityID":"1"},{"cityName":"Salt LakeCity","cityID":"2"}]

Here is the view.

<select data-bind="options: cities, optionsText:'cityName', selectedOptions: selectedCity""></select>

When I login I know I'm getting that string correctly however the select box has several blank spaces as it's options. I'm sure I'm missing something obvious here, but how can I get the array created by the php page and AJAX request into the observable array so it works? Thanks in advance for tolerating my ridiculousness.

5
  • Are you binding your viewmodel to your html?. On a side note, please do NOT validate and/or store user credentials in JavaScript! Commented Jul 11, 2012 at 8:26
  • Yes. I just didn't include the applyBindings line of code. Note that other bindings do work. The issue isn't with the bindings. It's with how the view model is interpreting the data returned in the AJAX request. Commented Jul 11, 2012 at 8:28
  • Have you tried logging the data when it is returned in the AJAX call? It is possible you need to parse it with JSON.parse(); Commented Jul 11, 2012 at 8:33
  • using json.parse did the trick. Make that an answer and I'll accept it for you. Commented Jul 11, 2012 at 8:41
  • I posted it below. Glad to help Commented Jul 11, 2012 at 20:36

1 Answer 1

1

You need to parse the result back to a JSON object, like:

$.get("dbhandler.php", { "fun": "cities"}, function (data) {
    self.cities(JSON.parse(data));
});
Sign up to request clarification or add additional context in comments.

2 Comments

Or better yet, just use $.getJSON which does both the HTTP GET and the parsing. api.jquery.com/jQuery.getJSON
funny enough when I tried that I wasn't getting what I wanted. I'll try it again, but this works for now. Thanks!

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.