Note that there are some explanatory texts on larger screens.

plurals
  1. PObackbone.js collection usage
    primarykey
    data
    text
    <p>I'm running into a problem maintaining my collection. First, I load attendees into a collection via fetch. This loads existing attendees from the database in to the collection. I also have a button which allows a user to add new attendees. When an attendee is manually entered it seems to wipe out the models loaded into the collection via fetch and starts fresh. All manually added attendees now populate the collection; however, i would like both the fetch loaded and manually added attendees to populate this list.</p> <pre><code>var InviteeView = Backbone.View.extend({ tagName: "tr", initialize: function() { this.collection = new InviteeJSONList(); _.bindAll(this, 'render','appendItem','remove','saveInvitee'); }, events: { "click .removeInvitee":"remove", "click .saveInvitee":"saveInvitee" }, render: function() { var source = $("#invitee-template").html(); var template = Handlebars.compile(source); var context = inviteeListJSON.attributes['json']; var html=template(context); $(this.el).html(html); return this; }, appendItem: function() { $("#attendees").append(this.render().el); }, remove: function() { $(this.el).remove(); }, saveInvitee: function() { var value = $(this.el).find('select').val(); var model = this.collection.attributes['json']['invitees']; model = model.filter(function(attributes) {return attributes.encrypted_id==value}); var attendee = new Attendee({ user_id: model[0]['id'], meeting_id: '&lt;?=$mid?&gt;', status: 'Uncomfirmed', first_name: model[0]['first_name'], last_name: model[0]['last_name'], email: model[0]['email'], user_uuid: model[0]['encrypted_id'], unavailable_dates: model[0]['unavailable_dates'] }); attendeeView.addAttendeeItem(attendee.attributes) this.remove(); } }); var AttendeeList = Backbone.Collection.extend({ model: Attendee, url: '&lt;?=$baseURL?&gt;api/index.php/attendees/&lt;?=$mid?&gt;&amp;timestamp=&lt;?=$timestamp?&gt;&amp;userid=&lt;?=$userid?&gt;&amp;apikey=&lt;?=$apikey?&gt;', parse: function(response) { if(response!="No History") { $.each(response['attendees'], function(key, value) { attendeeView.addAttendeeItem(value); }); $('.loading_attendees').hide(); } else { $('.loading_attendees').html("No attendees exists for this meeting."); } } }); var AttendeeView = Backbone.View.extend({ el: $('body'), initialize: function() { _.bindAll(this, 'render','fetchAttendees', 'appendItem', 'addAttendeeItem'); this.counter=0; this.collection = new AttendeeList(); this.collection.bind('add', this.appendItem); this.fetchAttendees(); }, events: { "click #addInvitee":"appendInvitees", }, appendInvitees: function() { var inviteeView = new InviteeView(); inviteeView.appendItem(); }, render: function() { }, fetchAttendees: function() { this.collection.fetch({ success: function(model, response) { }, error: function(model, response) { $('#loading_attendees').html("An error has occurred."); } }); }, appendItem: function(item) { var attendeeItemView = new AttendeeItemView({ model: item }); $("#attendees").append(attendeeItemView.render().el); attendeeItemView.updateAttendeeStatusSelect(); }, addAttendeeItem: function(data) { this.counter++; var attendee = new Attendee({ id: data['id'], user_id: data['user_id'], meeting_id: data['id'], status: data['status'], comments: data['comments'], attended: data['datetime'], first_name: data['first_name'], last_name: data['last_name'], email: data['email'], counter: this.counter, user_uuid: data['user_uuid'], unavailable_dates: data['unavailable_dates'] }); this.collection.add(attendee); }, }); </code></pre> <p>After the collection (2 items loaded from REST API) is loaded via fetch():</p> <pre><code>console.log(this.collection.models) outputs: [d] [d,d] </code></pre> <p>Then when I manually add an attendee via a button the collection seems to reset:</p> <pre><code>console.log(this.collection.models) outputs: [d] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload