Save data to server using BackboneJS - backbone.js

I am very new to BackboneJS. I am creating a simple CRUD application using BackboneJS.
I have seen an example on the web (http://backbonetutorials.com/videos/beginner/). I can see a GET request on load of application i.e. http://backbonejs-beginner.herokuapp.com/users to get all the users in JSON format.
I just wanted to know what is this url (http://backbonejs-beginner.herokuapp.com/users).
Can someone help me to understand where and how data is getting saved?
What do I need to do, if I want to do the same thing on my localhost?
Do I need to write any server side code in order for this to work?

The url in the example is (one of) the API endpoints that the demo application interacts with in order to function. Backbone.JS will allow you to fetch and save data in many different ways (by writing a connector yourself) but the default means of getting and setting Model data for backbone is through AJAX calls to a RESTful API.
So lets take a look at this code snippet:
MyModel = Backbone.Model.extend({});
MyModelCollection = Backbone.Collection.extend({
model: MyModel,
url: '/myserver/api'
});
Note the "url" configuration option on the Collection. That is the URL that Backbone will make calls to (GET,POST and others) in order to get and set data for the application. That URL should be provided by you in most cases - especially if you are the one building the application.
So in short...yes, you will need to write some server side code for your CRUD application to work. I would suggest watching the REST tutorial I linked above and then consulting these articles which provide a bit more detail about how Backbone models are supposed to work.
What is a Model
Collection URL option
Model URL option
Backbone Model - Save

The server side code is needed.
But if you just want some data not matter they are real or fake. What don't you just create a object with fake data and set the Model or Collection with them. This is more easier than setting up a backend.

Related

What are models in AngularJS?

I don't really understand what a model is in AngularJS. I know what a model is in the context of an MVC framework. For example in PHP I would create a model, like ApiModel.php or something like that, and in there I would put all sorts of cool stuff for the controller to work with.
This doesn't seem to be the way AngularJS thinks of models, in fact I can't find any good explanation of how to implement a model in AngularJS, but everywhere talks about them.
What is a model in AngularJS and how do I use them in the traditional MVC way?
A model in AngularJS specifically communicates with its associated views and controllers and notifies it whether there is a change in its state.
A more thorough description of what is a model can be found in the following link:
https://web.archive.org/web/20140502052028/http://www.webdeveasy.com/angularjs-data-model/
This article is relevant to your inquiry.
More specifically, he mentions that
Model classes encapsulate your application’s data and provide an API to access and manipulate that data. The other classes in your application will make requests of models via this API. When data on the model is updated, the model dispatches events that the other classes within your application can react to. Models are appropriate for capturing the domain logic, such as performing calculations or other manipulations.
A model is just the data that your application consumes.
If the data needs to be dynamic (i.e subject to CRUD operations) you would get this data from a remote source like a database (or even a flat-file) using angularjs' $http in a service or factory and then pass it to the Controller.
If you have a small app for read-only data, then you can hard-code it directly into a Controller.
The most popular data format for angularjs applications these days is JSON because of its flexibility.
The view (or front-end) would then access this data that lives on the scope in the Controller using ng-bind or ng-model or interpolation {{...}}

Are ASP.NET-MVC4 and AngularJs work well together?

Is the combination of technological ASP.NET-MVC4, and AngularJs work well?
About the needs of my app: most of the work of my application view pages with tables of information.
I need to have an easy option to identify each point in the tables on the client side, highlight it, paint it, and request additional information from the server about it.
ASP.NET MVC and Angular.js have nothing in common.
Short answer is yes - they work well together.
You would simply make all your Actions respond with JSON rather than HTML (unless you want HTML in some scenarios)
i.e you write your HTML template, angular controller, etc on the client, and have an Action to grab all the table records as JSON, then load that in your angular controller to populate the table, interact with it all on the client, and send any changes back to the server.
You could replace Angular with any client-side technology if you wanted. Backbone, Knockout, etc.

CakePHP: View without model from external API

I need help directing me to the best way to accomplish this. I have a third party API that I want to use for data. I want to call the API using AJAX to display information only when necessary so that I don't have to store it in the database.
I'd like to have a separate view or element so that the view is set up and the data returned can be placed into the view.
Should I create a controller to do this? Can you have a controller without a model? I'm sure this is not an ideal MVC method but I don't really know the best way.
What you write does not make any sense at all:
I want to call the API using AJAX to display information
You do not need anything on the php server side when you do an AJAX call to a 3rd party site. Your server and Cake app is not even called in this case. Just modify the returned data as needed and injected it in the DOM tree where needed.
Can you have a controller without a model?
Yes
public $uses = array();
If you would not want to use AJAX, MVC wise it would be the best to create a Datasource for the API and use it together with a model. The example on this page shows a complete implementation of using data from a remote source.

Where is the correct location for an AJAX call in Backbone.js?

I'm learning Backbone.js for a new app I'm building.
I need to perform an AJAX call to get more properties for my model object (image, title & description of a book).
Where is the correct place for this call? In the Model, View or somewhere else? specifically related to Backbone.js MVC model.
Additional Info
I'm using LocalStorage and not a server. The AJAX call will be to a web service API and constitutes only part of my actual model data.
If you have properly configured your model you can just call model.fetch() which will issue a HTTP GET to whatever url you have configured and refresh the model with the results from the server.

mvc programming question

Am using a view file,controller and a helper. am accessing data value through webserver.
Steps:
the controller get value from webserver and set it in the view. the view uses the helper to display the data in some format. But my helper again calls the webserver method to get the inner values. Is it correct the helper accessing webservice method? Is it the correct way of programming in mvc?
Thanks,
IMO, a webservice is just another datasource and should be accessed via the model. If it's me, I handle it by either creating a new model for the service call (if the service call is in support of an existing entity, it may make more sense to make the call in that entity's model itself). My controller calls the model method, sends the data to my view which, in turn, forwards that data on to the helper.
This maintains the MVC separation, but still allows the data you need to make it's way into the helper where you need it.
I will tell you what is written in the Ruby on Rails book. I can not remember the title right now but...
Helpers are usually used for view rendering not for server calls.
Hope it helps.

Resources