Wie kann ich das Rendern einer Backbone-Sammlung in einer Liste und Element-Anzeigen?

Arbeite ich an einer Kontakt-bar, die macht alle Kontakte eines Benutzers in einer html-Liste.

Was ich habe:

  1. UserModel - Dies ist eine einfache Backbone.Modell mit Benutzernamen und E-Mail
  2. UserCollection - Dies ist der Kontakt-Liste
  3. ContactsView - das ist der ul-Kontaktliste
  4. ContactView - Dies ist ein single-Kontakt-Modell gerendert li

Ich bin derzeit bricht meinen Kopf über eine Lösung, wie (und wo) kann ich Holen meine UserCollection und wie gebe ich den einzelnen Modellen bis hin zu einzelnen ContactView Element.

Spezifischen Hürden sind:

  1. Wo sollte ich Holen, speichern Sie die UserCollection
  2. Wie kann ich machen das Kontakt-Liste
  3. Wie kann ich machen das Kontakt-Elemente
  4. Wie kann ich verhindern, dass fetch({ success: callback }), aus brechenden meine code-Struktur

Mein aktuelle code ist:

Eingang Punkt:

//create a new instance of the contact list view
var view = new ContactsView();
//insert the rendered element of the contact list view in to the dom
$('div.contacts-body').html(view.render().el);
view.fetch({ success: view.loadContacts });

ContactsView:

define(
    ['jquery', 'underscore', 'backbone', 'text!templates/conversations/contacts.html', 'collections/users', 'views/conversations/contact'],
    function($, _, Backbone, ContactsTemplate, UserCollection, ContactView) {

        var ContactsView = Backbone.View.extend({

            tagName: "ul",

            className: "contacts unstyled",

            attributes: "",

            //I am feeling uneasy hardcoding the collection into the view
            initialize: function() {
                this.collection = new UserCollection();
            },

            //this renders our contact list
            //we don't need any template because we just have <ul class="contacts"></ul>
            render: function() {
                this.$el.html();
                return this;
            },

            //this should render the contact list
            //really crappy and unflexible
            loadContacts: function() {
                this.collection.each(function(contact) {
                    //create a new contact item, insert the model
                    var view = new ContactView({ model: contact });
                    //append it to our list
                    this.$el.append(view.render().el);
                });
            }

        });

        return ContactsView;

});

ContactView

define(
    ['jquery', 'underscore', 'backbone', 'text!templates/conversations/contact.html'],
    function($, _, Backbone, ContactTemplate) {

        var ContactView = Backbone.View.extend({

            tagName: "li",

            className: "contact",

            attributes: "",

            template:_.template(ContactTemplate),

            initialize: function() {
                this.model.bind('change', this.render, this);
                this.model.bind('destroy', this.remove, this);
            },

            render: function() {
                this.$el.html(this.template(this.model.toJSON()));
                return this;
            }

        });

        return ContactView;

});

Könnte jemand mir helfen, über meine vier Hürden.

Gutes Beispiel links sind willkommen. Ich orientierte meinen code-Stil im todos-Liste leider todos-Liste ist nicht, dass fortgeschrittene...

AKTUALISIERTEN CODE:

define(
    ['jquery', 'underscore', 'backbone', 'text!templates/conversations/contacts.html', 'collections/users', 'views/conversations/contact'],
    function($, _, Backbone, ContactsTemplate, UserCollection, ContactView) {

        var ContactsView = Backbone.View.extend({

            tagName: "ul",

            className: "contacts unstyled",

            attributes: "",

            events: {

            },

            initialize: function() {
                this.collection = new UserCollection();
                this.collection.on('reset', this.render);
                this.collection.fetch();
            },

            render: function() {
                //in chromium console
                console.log(this.el); //first: html, second: undefined
                console.log(this.$el); //first: html in array, second: undefined
                this.$el.empty(); //error on the called that this.$el is undefined

                this.collection.each(function(contact) {
                    var view = new ContactView({ model: contact });
                    this.$el.append(view.el);
                }.bind(this));

                return this;
            }

        });

        return ContactsView;

Kann es sein, dass der reset ausgelöst wird dieser.render zweimal?

InformationsquelleAutor bodokaiser | 2012-07-25
Schreibe einen Kommentar