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 8273743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:33:41+00:00 2026-06-08T07:33:41+00:00

I’m new to Backbone.js and am having trouble figuring out the proper architecture for

  • 0

I’m new to Backbone.js and am having trouble figuring out the proper architecture for a model-view relationship.

I have a view that holds an input box and a model that is supposed to take the contents of that input box and send it to the server.

My issue is that I don’t always have a discreet DOM event that triggers a request for the view to update the model data, such as input.change. Sometimes the code itself needs to ask the model to send updates to the server.

I’ve thought of three solutions to this problem so far, I’m not sure if any if them are any good though:

  • Update the model on the input element’s keypress event

  • Once the view is initialized with the model, have the view update/add a function to the model called ‘get_input_value()‘ that returns the value of the input box

  • Whenever the application needs to request the model to update the server, first call a function in the view that updates all of the information that the user has typed into the view to the model.

Please bear in mind that this is a simplified example. The view contains child views as well, all of which hold a number of elements that the user can manipulate, the model needs to be updated with all of this information so that it can update the server.

Any help and input is appreciated! Thanks so much!

Edit :::

Base on machineghost’s response, I now see that I did not articulate this problem correctly:

There is a DOM event, but the problem is that it doesn’t necessarily originate from inside the view that uses the model. It may originate from the Router or another view and be triggered by a global event handler. Additionally, there is not a 1:1 View-Model relationship. This model is used by multiple views who express the model in different ways. So in this case, it seems like the command to update the server should not go through a View, but to the model itself. If that is the case, the model must be able to say “Sync me with my views!”.

But I don’t know how to do this without breaking the rules and thus creating other problems with architecture…

  • 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-08T07:33:43+00:00Added an answer on June 8, 2026 at 7:33 am

    Ok this is kind of a subjective question, so forgive me if this just seems like me spouting off my two cents. And before I even answer your question, I have to admit I’m a bit skeptical that you:

    don’t always have a discreet DOM event

    because pretty much anything the user can do triggers an event that you can watch for. For instance, if you want to wait until a user changes a text input there’s change, but also (as you noted) the various key* events, plus there’s blur (which is commonly used for this sort of thing). Between the 3(+) you should always be able to respond appropriately to the user’s actions. It would only be if (say) you had to save the text input’s contents every 3 seconds that it would truly be independent of DOM events.

    So, without knowing your particulars, I just have to point out that something smells fishy there. But anyhow, as for your actual question, here’s my take on your ideas:

    Update the model on the input element’s keypress event

    This certainly would work, but just be sure to use the view to do the actual event handling/model setting; hooking up the onKeyPress handler in the model would be a bad idea

    Overall this approach seems pretty standard, and fits the Backbone paradigm.

    Once the view is initialized with the model, have the view update/add a function to the model called ‘get_input_value()’ that returns the value of the input box

    I don’t quite get how this helps your problem, plus it seems to put the concerns in the wrong place: the model should (ideally) have nothing to do with the DOM.

    Whenever the application needs to request the model to update the server, first call a function in the view that updates all of the information that the user has typed into the view to the model.

    Is the save happening every 5 minutes or something? If not, then it’s presumably happening in response to the user’s actions, and you should use an event handler to respond.

    However, if you truly do need to make the sync independent of user actions, I’d recommend using a custom event to manage things. In other words, in your model’s sync method put something like this.trigger('preSync'). Then, every view which uses that model can bind some sort of updateMyModelValue method, ie. this.model.on('preSync', this.updateMyModelValue, this);.

    This way, your model code is never directly interacting with the DOM at all; instead, it just worries about the stuff it’s supposed to worry about (the data) and the views pay attention for when they need to update that data from the DOM.

    Hope that helps.

    * EDIT (in response to your editing of your question) *

    If that is the case, the model must be able to say “Sync me with my views!”.

    The general Backbone way for a model to say … well, pretty much anything to its views is through events.

    (Technically you could maintain a list of a model’s views in the model itself, and then iterate through that list to tell the views to do things. Backbone is even un-opinionated enough to let you do that. However, from a maintainability standpoint that seems like a terrible approach to me.)

    My example of a “presync” event (above) demonstrates how you’d use this technique; comment back if any of it is unclear.

    Similarly, if you have an issue of:

    1. View A catches an event
    2. View B needs to do something in response to that event

    You basically have two options:

    1) You can tightly couple the two views. Let’s say have a table view that creates row views, but needs to respond to events that happen in those rows. You can pass the table itself as an option to the row when you create it (new Row({table:this})), and then when those rows need to tell their table “an event happened” they can just do this.options.table.informThatAnEventHappened(). This is a great approach if the two views are inherently related, like a table and its rows. If not, a better approach is:

    2) You can use events to communicate between the views. Let’s say you have a title div at the top of the page, which needs to be updated whenever a “title” text input changes … but that text input is way down the page and doesn’t conceptually have much to do with the page’s title (apart from setting it). The common point between these two elements (and their views) is the data, the text of the title itself.

    Now imagine that titleDivView and titleSettingInputView both share a pageTitle model. When titleSettingInputView calls this.model.set('titleText', 'newTitle'), the titleDivView can listen for this.model.on('change:titleText', ...), and re-render itself appropriately in response. In this way two totally un-connected, de-coupled views can interact with each other, without creating a tangled web of inter-related code.

    And of course, if there isn’t a nice convenient “change:title” event to bind to, you can always make your own, as with the custom “presync” event I described above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I know there's a lot of other questions out there that deal with this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.