سؤال

أنا أشعر بتغذية JSON من API مريح.JSON أحصل على من الخادم هو كائن مع صفيفين مختلفين من الكائنات.أنا أقحم أن JSON في مجموعات عمودية مختلفة تعمل بشكل جيد.تبدو كائنات العمود الفقري الخاص بي مثل هذا: giveacodicetagpre.

الكائنات نفسها على ما يرام، لكنني أحاول استخدام المقاود .js للحصول على القالب وقد وجدت يجب أن تأخذ كائن، وليس صفيف.عندما آخذ كائن المستندات الخاصة بي وقم بتنفيذ .tojson ()، يبدو أنه يعيد مجموعة، وليس كائنا.لذلك يشبه JSON RAW من الخادم: giveacodicetagpre.

ولكن بمجرد أن يتم دفعها إلى مجموعة العمود الفقري، تم سحبه مرة أخرى من أجل مقود المقود، يبدو هذا: giveacodicetagpre.

كيف يمكنني الحصول على العمود الفقري لإعطائي الكائن بدلا من مجرد صفيف مع محتويات الكائن؟

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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}}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top