jamessugrue / beginning-backbone

Code extracts from the upcoming Apress title, Beginning Backbone
34 stars 22 forks source link

Problem resetting/clearing search query #3

Closed davidsilva closed 9 years ago

davidsilva commented 10 years ago

Hi, James,

I hope you can help me with this. Search works correctly the first time but subsequent search queries are appended to the URL in the API call (they don't appear in the browser URL, though). For example, here's this console output:

Run search against keith SearchView.js:31 search for keith AppRouter.js:26 XMLHttpRequest cannot load http://localhost:8080/search/keith/keith. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I think the problem is in the AppRouter.

davidsilva commented 10 years ago

With the help of a friend, I solved the problem:

var com = com || {}; com.apress = com.apress || {}; com.apress.model = com.apress.model || {};

com.apress.model.Search = Backbone.Model.extend({

url: 'http://localhost:8080/search',
rootUrl: 'http://localhost:8080/search',

sync: function(method, model, options) {

    if (this.get('query')) {
        this.url = this.rootUrl + '/' + this.get('query');
    }
    Backbone.sync.call(this, method, model, options);
},

parse: function(model) {

    return model;
}

});

By having this.url = this.url + ... subsequent queries were being concatenated -- and thus failing.

jamessugrue commented 10 years ago

Thanks for spotting this! I will update the source