Exploring Backbone with exercise by trying to obtain a JSON file from the server using the urlRoot
property of the Model
.
Encountered an error (404) when setting urlRoot: "./js/json/todo"
, paths with ".json" work but
console.log(todoItem.get('description'))
returns undefined
.
In most Backbone applications, omitting the extension .json
seems common e.g. urlRool: "/todos"
rather than urlRoot: "/todos.json"
, yet errors are experienced without the extension.
- Seeking guidance on correct use of
url
andurlRoot
properties. - Interested in learning about implications of not using
.json
or including it. - Lastly, seeking feedback on whether the JSON file format below is suitable for Backbone?
Backbone:
(function(window, $, Backbone) {
var TodoItem = Backbone.Model.extend({ urlRoot: './js/json/todos' });
var todoItem = new TodoItem({id: 1});
todoItem.fetch();
console.log(todoItem.get('description')); // Returns undefined
}(window, jQuery, Backbone));
JSON:
{
"todos": [
{
"description": "Pick Up Milk",
"status": "incomplete"
},
{
"description": "Do shopping at Market",
"status": "incomplete"
}]
}