Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9111689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:33:56+00:00 2026-06-17T03:33:56+00:00

I’m building an application in Backbone. In my collection model, I have a sync

  • 0

I’m building an application in Backbone. In my collection model, I have a sync function that fires when a user clicks a button. It submits some form data to the server and the server returns a JSON object containing either a success or error message.

The problem is that I can’t seem to trigger an event in the collection view when the collection model syncs:

Here is the Complete Script:

  var RegisterModel = Backbone.Model.extend({url: 'api/process.php', defaults: { contentType: 'input', value: '', inputType: 'text', inputClass: 'required', serverResponseClass: 'alert alert-error', message: ' ', responseType: ' '} });



var RegisterView = Backbone.View.extend({

    template: _.template('<p><% if (contentType == "input") { %><input placeholder="<%= label %>" name="<%= inputName %>" id="<%= inputName %>" value="<%= value %>" type="<%= inputType %>" class="<%= inputClass %>"/><% } %><% if (contentType == "button") {%> <input type="<%= inputName %>" class="<%= btnClass %>" id="<%= inputName %>"<% } %></p>'),

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




var RegisterCollection = Backbone.Collection.extend({ 
    model: RegisterModel,

    url: 'api/process.php',

    updateModels: function (){
        this.forEach(this.updateModel, this);
    },

    updateModel: function(registerModel) {
        var inputName = '#'+registerModel.get('inputName');
        var userInput = $(inputName).val();
        registerModel.set({value: userInput});
    },
    clearErrors: function() {
        var musketeers = this.where({contentType: "serverResponse"});
        this.remove(musketeers);
    },
    syncCollection: function() {
        Backbone.sync.call(this, 'create', this);
    }
});




var RegisterCollectionView = Backbone.View.extend({
    initialize: function(){
        this.collection.on('reset', this.addAll, this);
        this.collection.on('add', this.addAll, this);
        this.collection.on('sync', this.onModelSaved);

        /* this.collection.on('remove', function(){registerCollection.fetch(); }, this); */
    },
    el: "#backboneContainer",

    events: {
        "click #submit" : "validate",
    },

    validate: function(e) {
        e.preventDefault();
        if ($("#register").valid() === true) {      
            this.collection.clearErrors();
            this.collection.updateModels();
            this.collection.syncCollection();
        }
    },

    errorOverlay: function(errorTitle, errorContent) {  
        $.notification({
            title: errorTitle,
            content: errorContent,
            timeout:    5000,
            icon: "!",
            error: true,
            border: false
        });
    },

    onModelSaved: function () {
        console.log('hello world!');
        alert('hello world!');  
    },

    successMessage: function(message) {
        $.fn.modal({
            layout:     "elastic",
            url:        undefined,
            content:    message,
            padding:    "50px",
            animation:  "fadeInDown"
        });
        this.$el.append('<span class="icon">=</span>');
    },

    addOne: function(registerModel){
        var registerView = new RegisterView({model: registerModel});
        this.$el.append(registerView.render().el);  
    },

    buildForm: function(registerModel) {
        var registerView = new RegisterView({model: registerModel});
        $('#register').append(registerView.render().el);
    },

    addAll: function(){
        this.$el.empty();
        this.$el.append('<div class="con"><div class="section current" title="Our First Section" ><div class="col_12"><h1>Piecharter.com</h1></div><div class="carton col_4"><h2>Registration</h2><div class="content"><form id="register" method="post"></form></div></div></div></div>');
        this.collection.forEach(this.buildForm, this);
        $("#register").validate();
    /*  this.$el.append('); */
    },

    render: function(){
        this.addAll();
        return this;
    }
});



$('.modal').live("click", function () {
    this.remove();
    $('#overlays').hide();
});



/*VALIDATION HERE ~*/

jQuery.validator.addClassRules("username", {
   minlength: 7,
   maxlength: 21
});

jQuery.validator.addMethod("password", function( value, element ) {
    var result = this.optional(element) || value.length >= 1 && /\d/.test(value) && /[a-z]/i.test(value);
    if (!result) {
        var validator = this;
        setTimeout(function() {
            validator.blockFocusCleanup = true;
            element.focus();
            validator.blockFocusCleanup = false;
        }, 1);
    }
    return result;
}, "Your password must be at least 7 characters long and contain at least one number and one character.");

I know the collection is successfully syncing, so the problem must be with the way I am listening to the sync event in my collection view. Any ideas what is wrong here? I’ve looked over the documentation and I can’t find any problems with the way I have structured this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:33:57+00:00Added an answer on June 17, 2026 at 3:33 am

    You left out the this when you tried to bind “onModelSaved”, so instead of referencing the object’s “onModelSaved” method, you referenced the global “onModelSaved” variable (which almost certainly doesn’t exist).

    Try:

    this.collection.on('sync', this.onModelSaved);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.