Not getting model results correctly

Apparently new users may include only 1 image per post :slight_smile:

So the following image contains:

  • My web-api return values.
  • My model is definition (I have tried overriding theparseModels and parseModel with no success).
  • The results — seems to split the string into an array.

What am I doing wrong?

can.Model expects each item in a list to be an object, not a String (or other primitive). So it expects your data to look like:

{
 data: [{id: 1, value: "something"}, {id: 2, value: "something else"}
}

This is because can.Model creates an instance for each item in the list. You can’t really create instances “around” primitives. I suppose we could create computes here, but this is not a common service result.

So the solution is to turn those strings into an object. The following should work:

id: "value",
parseModel: function(){
  return {value: value}
}

However, I’m concerned that the service topology you’re building on top of is a bit awkward. I’m assuming annonymous permissions is the current user’s permissions?

If it is not. Then my guess is that it’s a list of all permissions available. If this is the case, then what you are doing is ok.

If it is the users current permissions, perhaps consider the following:

In apps that I can direct the service layer, I typically have a /session service that I can access like (with can.Model):

Session.findOne({}).then(function(session){
  session.attr("user").attr("permissions");
})

This means that /session returns data like:

{expires: 2123123131, user: {name: "Justin", permissions: ["add://page1"]}}

Session defines its related User type like:

Session = can.Model.extend({
  findOne: "/session"  
},{
  define: {
    user: {type: User}
  }
})

I have the application view model call out to session like is done here:

Ah, I see. I guess that makes sense given that it is what the documentation says :slight_smile:

The anonymous permissions are specified to allow a non-logged on user to access certain functionality. As this product is intended for commercial use the security mechanism needs to be quite flexible.

I do have a session, though.

I did notice that you seem to like to place application service functionality directly on models and even the application state. I’m still learning canjs so perhaps some of the philosophy behind your thinking may rub off. I really do not mind refactoring as I go along so we’ll see.