1
votes

My first post so please go easy on me..

I am currently writing a hospital rostering web application in BackboneJS + JAX-RS. It has been ok until now but I can't for the life of me work this one out...

In my Group model I have a Users collection.

This is returned as a List<User> from JAX and User has the @XmlRootElement annotation on it.

But when I call fetch it fills the Group model with an array of User models instead of a Users collection.

Is there a way to tell JAX to return my list of User models as a Users collection?

Any help appreciated!

EDIT:

Group model in Backbone:

window.Group = Backbone.Model.extend({
urlRoot: "api/groups",
defaults: {
    name: ''
},

validate: function(attrs){
    var errors = [];

    if (!attrs.name) {
        errors.push({name: 'name', message: 'Please fill in the name field.'});
    }

    if(!attrs.users || attrs.users.length == 0){
        errors.push({name: 'users', message: 'Please add at least 1 user.'});
    }

    return errors.length > 0 ? errors : false;
}
});

Group model in Java:

package org.jakeduncandev.roster;

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Group {

private int id;

private String name;

private int ownerid;

private List<User> users;

public List<User> getUsers() {
    return users;
}

public void setUsers(List<User> users) {
    this.users = users;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getOwnerid() {
    return ownerid;
}

public void setOwnerid(int ownerid) {
    this.ownerid = ownerid;
}


}

JSON returned:

"groups":[{"name":"mates","id":71,"users":[{"id":6,"password":"<PASSWORD>","email":"<EMAIL>","firstName":"Jake","lastName":"Duncan"},{"id":7,"password":"<PASSWORD>","email":"<EMAIL>","firstName":"alec","lastName":"stearn"}],"ownerid":0}]
1
If you post your code, you'll more likely get some answers. - Pappa
you have to override your your model's parse function to set data to collection. - user10
can you please post your Group model and the JSON you're receiving? - providencemac
Thanks for the comments. The JSON returned is with only 1 group that has 2 users in it (i took out password and emails). users10 - how do I use the parse function for a collection? I am not using fetch, because I return the users groups upon initial authentication and initialise the collections by passing in the returned array of models. Any more help would be appreciated! - jakedunc

1 Answers

0
votes

SOLVED!

I added a parse method to my Group model. And then when I was instantiating my collection I used the option: {parse: true} and then it calls the parse method for each model.

Thanks for your help everyone and to user10 for suggesting overriding the parse method!

parse: function(response){

    response.users = new window.Users(response.users);

    return response;
}