Frage

Ich erhalte einen JSON-Feed von einer erholsamen API.Die JSON, die ich vom Server bekomme, ist ein Objekt mit 2 verschiedenen Objekten von Objekten.Ich brach diesen JSON in ein paar verschiedene Rückgratsammlungen, die gut funktioniert.Meine Rückgratobjekte sehen so aus: generasacodicetagpre.

Die Objekte selbst sind in Ordnung, aber ich versuche, Lenker zu verwenden.Wenn ich mein Dokument-Objekt nehme und ein .tojson () mache, scheint es ein Array zurückzugeben, kein Objekt.Also sieht der rohe JSON, den ich vom Server bekomme, sieht aus wie: generasacodicetagpre.

Aber sobald es in die Backbone-Sammlung geschoben wird, dann für die Lenkerübergabe herausgezogen, sieht es so aus, dass es so aussieht: generasacodicetagpre.

Wie kann ich Backbone bekommen, um mir das Objekt zurückzugeben, anstatt nur ein Array mit dem Inhalt des Objekts?

War es hilfreich?

Lösung

backbone gives you an array from a collection because that is what the collection holds -- an array of models.

I'm not sure I'm clear on exactly what you're trying to do, but it looks like all you actually need is to have the server receive a document that looks like:

{
    "Docs": docCollection.toJSON()
}

If this is the case, I'd just override the toJSON method on your Documents class to do something like:

toJSON: function() {
    return {"Docs": Backbone.Collections.prototype.toJSON.apply(self, arguments)};
}

This should pretty much do what you say you want, at least as I understand it.

Andere Tipps

Unfortunately Backbone collections don't play as nicely as they should with handlebars. Since it returns a plain JavaScript object you have to reference it as such.

Try:

{{#each []}}
  <tr>
    <td>{{ this.title }}</td>
    <td>{{ this.start }}</td>
    <td>Invited</td>
    <td>Confirmed</td>
    <td>Edit</td>
  </tr>
{{/each}}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top