Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAL.Collection doesn't support direct instantiation with a JSON array #8

Closed
nickahoy opened this issue May 14, 2013 · 0 comments
Closed

Comments

@nickahoy
Copy link

Monkey = HAL.Model.extend({});
Monkeys = HAL.Collection.extend({ model: Monkey });

monkeys_list = [
  {
    "_links": {
      "self": {
        "href": "/zoo/monkeys/1"
      }
    },
    "id": "1",
    "name": "Petunia",
    "traits": [
      "throws poo",
      "hosts tea parties"
    ],
    "_embeded": {
      "events": [
        {
          "_links": {
            "self": {
              "href": "/zoo/events/343"
            }
          },
          "date": "2012-09-11",
          "description": "Threw a tea party for the Queen. After sharing some particularly lovely shortbread biscuits, started throwing poo."
        },
        {
          "_links": {
            "self": {
              "href": "/zoo/events/301"
            }
          },
          "date": "2011-12-19",
          "description": "Gave a lovely teapot to a child for Chistmas"
        }
      ]
    }
  },
  {
    "_links": {
      "self": {
        "href": "/zoo/monkeys/3"
      }
    },
    "id": "3",
    "name": "Lancelot",
    "traits": [
      "aspires to be a space monkey",
      "dabbles in astronomy and astrophysics"
    ],
    "_embeded": {
      "events": [
        {
          "_links": {
            "self": {
                "href": "/zoo/events/444"
            }
          },
          "date": "2013-03-28",
          "description": "Acted as copilot on the Soyuz TMA-08M mission to the ISS."
        },
        {
          "_links": {
            "self": {
                "href": "/zoo/events/45"
            }
          },
          "date": "2009-07-02",
          "description": "Repremanded after firing a prototype rocket into the nearby Gazelle aviary."
        }
      ]
    }
  }
];


> monkeys = new Monkeys(monkey_list)
  child {links: Object, embedded: Object, attributes: Array[2], length: 0, models: Array[0]…}
> monkeys.models
  []
> monkeys.length
  0

If the Monkey model and collection were standard Backbone objects (instead of HAL objects), you'd get the following:

> monkeys.models
  [ child, child ]
> monkeys.length
  2

Resetting the collection with the JSON array works as expected:

> monkeys = new Monkeys()
> monkeys.reset(monkeys_list)
  child {links: Object, embedded: Object, attributes: Array[2], length: 2, models: Array[2]}
> monkeys.models
  [ child, child]
> monkeys.length
  2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant