What is backbone js
Development with Backbone.js
A current project was about the implementation of a quiz application. 10 questions should be answered one after the other - but the page should not be left or reloaded. The first thought was of course: "Will somehow work with jQuery". But isn't it easier than constantly manipulating DIVs by hand?
Yes, that is easier and better. With Backbone.js.
What is Backbone.js?
This article is intended to give you a first overview of how the development with Backbone.js works and which possibilities are offered by this framework.
The application in detail
Back to the application itself. As mentioned, it is a quiz with 10 questions. There is a picture for each question and three possible answers. During the entire quiz, the user sees the number of correctly answered questions. Here we go!
First up are the classes question and game created. The class game serves as a kind of administration with the number of correctly answered questions etc. The class question contains the specific information on the individual questions.
Now should that too game be represented as a model.
An event listener is registered directly in the constructor. This automatically updates the number of correct questions in the view as soon as the user answers a question correctly. The self-defined method "get_next_question ()" renders the view of the next question.
From my point of view, these views are one of the highlights of Backbone.js!
First, the view class is created in the backbone.
The DIV “#question_show_template” is used in the render function. This is the actual HTML view (see below).
Click events, such as a click on the "Answer question" button, are defined directly in this class. over this.variables all attributes (of the question) can be accessed in every method of the View class.
Example: About this.variables.correct one learns the expected answer to this question (and can then compare this with the user input).
What is still missing is the mentioned HTML view. After all, Backbone.js needs to know exactly how the data should be presented.<div class="row"> <div class="large-12 columns"> <img src="<%= image_url %>" /> </div> </div> <div class="row"> <div class="large-12 columns"> <p><%= question %></p> <div> <input type="radio" name="answer" value="1"> <%= answer_a %> <input type="radio" name="answer" value="2"> <%= answer_b %> <input type="radio" name="answer" value="3"> <%= answer_c %> </div> </div> <button id="next">Jetzt antworten</button> </div>
Each time the view is rendered, the attributes are automatically inserted into the HTML. So here you don't have to manually change all DIVs individually with jQuery, but only define this template once. Backbone.js does the rest. The whole thing is also W3C-compliant!
The development workflow summarized
The development with Backbone.js is really quick and from my point of view saves work in the long term. First, the models and their relationships are modeled in the backbone. These are then filled with life at runtime.
The whole thing is very modular, clear and maintainable. The questions are simply created in a text file, for example. You could also have lists or menus generated dynamically in this way.
The ability to define view templates also saves a lot of work. The template is created once - the rendering and filling of the DIVs is completely taken over by Backbone.js.
The "C" from MVC should also be emphasized here. Via simple calls such as Save() or get () Backbone.js sends a corresponding AJAX request via a RESTful JSON interface. In this way, data can be saved or loaded on the server without leaving the page. I see this as a very useful functionality, especially for single-page applications.
In retrospect, I see the use of the framework in this project as very useful. Of course you have to put some time into modeling. In return, you save an enormous amount of time with subsequent maintenance and an enormous amount of headaches when handling jQuery selectors.
Do you already have experience with Backbone.js? Could you imagine an added value from developing with Backbone.js?We look forward to your opinions and experiences!
- Can coffee promote creativity?
- What is the opposite word of banal
- Are social scientists hired as data scientists?
- Why is it called an urban legend
- What does Intel Core i5
- What does communication technology do?
- What should I not miss in Trivandrum
- How much does Norco cost without insurance
- Who is better Alexandre Lacazette or Aubameyang
- What does careful politician time mean
- Boxelder bites bugs
- How did Richard Mueller meet his wife
- Can you write some autumn poems
- What is a ranch 2
- How are scones and rock buns different
- Are there any free products out there
- What are analysis tools
- A good razor that hairdressers use
- What is the full form of MTS
- What car insurance is needed in Mexico
- What are some emotional US TV series
- Why does terror not have a religion?
- Is a brain injury permanent
- Which is better Heritage or KGEC