0

I am having a service point implemented with ASP.NET web API. It returns a list, an IEnumerable of dto's embedded within HttpResponseMessage.

However my client code which is implemented via backbone collection fetch always end up with error. Inside the error method, while I pass & watch the parameter I see the collection is appears under ResponseText property.

While I directly pass IEnumerable<dto> without wrapping it with HttpResponseMessage, it works fine.

My question: What is the correct way? How can I make my backbone collection refer to what inside ResponseText property?

I try to play around to make a single model fetch, and override fetch method.

categories = new Categories({ url: 'http://localhost:6685/category' });

$.when(categories.fetch())
    .then(
        function() {
               app.init({ 'categories': categories, 'cart': cart });

               });

This keep giving me ambiguous errors. then when i switched with

categories.fetch({
            success: function() {... },
            error: function(err) {

                console.log(err);
            }
        });

I can see inside err my categories list. the property name is responsetext.

var Categories = Backbone.Collection.extend({

    model: Category,

    initialize: function (options) {
        this.url = options.url;
    },

    parse:function (response) {
        return response.categories;
    },

Server-side

HttpResponseMessage<IEnumerable<category>> httpresponse = null;

 var response= _catalogueservice.getcategories();

            if (response.success)
            {

                httpresponse =  new HttpResponseMessage<IEnumerable<category>>(response.categories, response.categories.Count()>0?HttpStatusCode.Found:HttpStatusCode.NotFound);
            }
            else
            {
                httpresponse = new HttpResponseMessage<IEnumerable<category>>( HttpStatusCode.NotFound);

            }

        return httpresponse;

I can see under fiddler that JSON is being returned.

1
  • the problem seems to be HttpStatus Code, Ajax requests for Backbone always expect HttpStatusCode.OK if correctly executed otherwise non 200 status code. Just change your code to this httpresponse = new HttpResponseMessage<IEnumerable<category>>(response.categories, response.categories.Count()>0?HttpStatusCode.OK:HttpStatusCode.NotFound); May 17, 2012 at 14:23

1 Answer 1

1

Always keep your services Dry simple. You are into nightmare when you find out most of the api users have to use another wrapper around data just because of your convenience. Anyways keep that aside below is all that you have to do

var todos = Backbone.Collection.extend({
url:"todoapi/gettodos",
parse:function(response){
   return response.Todos
}
});

taking into account your response looks like below

{Error:"NULL",Todos:[{Id:1,Todo:"Welcome"},{Id:2,Todo:"Eat"...}]}
2
  • thanks for advise, it s supposed to be a web service point, would it be correct assume httpresponsemessage at some point is a generic format. anyhow the parse method never get fired since fetch return an error. am i still missing something? i am using httpstatuscode should i embed at somewhere error:null explicitly?
    – mobygeek
    May 17, 2012 at 10:29
  • @mobygeek it would be easy for me to put a right answer if you include some code with your question. May 17, 2012 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.