Models Flashcards Preview

Backbone > Models > Flashcards

Flashcards in Models Deck (30):
1

How do you set an attribute on a model?

model.set('title');

2

How do you get an attribute on a model?

model.get('title');

3

How do you get a JSON representation of a model?

model.toJSON();

4

How do you clear all attributes on a model?

model.clear();

5

How do you unset an attribute on a model?

model.unset();

6

How do you see if a model has an attribute?

model.has('title');

7

How to create a model?

var Model = Backbone.Model.extend();

8

How do you instantiate a model?

var model = new Model;

9

How do you set values to be a default when coding a model?

var Song = Backbone.Model.extend({
defaults: {
title: "default value"
}
});

10

How do you use validation on model attributes?

* You need to implement the validation method in your model
* validate is called anytime the attributes are initially set.
* Validate
* attrs is a json object representing the models given attributes
* You can check if said attribute is set, etc.
validate: function(attrs) {
if (!attrs.title)
return 'The song requires a title';
}

11

How can you check if a model is valid? Which checks validate

model.isValid() // function returns true || false

12

If you call isValid on a model how do you get the last validation error message?

You call model.validationError on a model after you have called isValid that will be available

13

true or false - You can use inheritance to extend your custom models?

true

14

How do you extend you custom models?

Animal.extend() just like extend Backbone.Model

15

true or false - You cannot override a parent method on an extended class?

false

16

How do you override a parent method in an extend object?

Use the same method name and write new code.

17

How can you call the parent class method from a sub class?

Javascript doesn't provide an easy way, but you can do...
ClassName.prototype.methodyouwantocall.apply(this)

18

What are the 3 persistence operators a backbone model has?

fetch() - GET
save() - POST/PUT - new POST, update PUT
destroy() - DELETE

19

How does Backbone know if it's a PUT or a POST?

If it's been fetched from the server before it's a PUT, and if it hasn't before then Backbone thinks it's a new item and does a POST

20

What property do you use to tell models where to look on the server?

urlRoot: '/api/tasks'

21

What property is used to fetch a model from the database?

id: 1 when instantiating the model.
var song = new Song({ id: 1 })
results in /api/songs/1 etc

22

true/false Backbone routes are restful by default?

TRUE

23

how would you do a PUT request to the server?

var song = new Song({ id: 1 });
song.fetch();

song.set('Title': 'Hello world');
song.save();

api/songs/1 PUT request;

24

How would you create a new model and persist it?

var song = new Song();
song.set('Title', 'Hello world');
song.save();

api/songs POST request persisting a new item.

25

How would you delete a model from the database?

var song = new Song({ id: 1 });
song.destroy();

api/songs/1 DELETE request to the server.

26

true/false You can use a custom unique identifier for a model?

TRUE

27

How do you tell Backbone to use a different unique identifier besides 'id' to identify the model?

idAttribute: 'songId' or 'song_id' property

and then when getting a model you would use

var song = new Song({ songId: 1 });

28

true/false Are all database calls asynchronous and have a success and error callback?

TRUE

29

How do you access the error/success call backs for fetch/destroy?

var song = new Song({ id: 1 });
song.fetch({
error: function() {}
success: function() {}
});

same for song.destroy();

30

How do you access the error/save callbacks on the save method?

var song = new Song({ id: 1 });
song.save({}, {
error: function() {}
success: function() {}
});

NOTE: the first argument is a hash of attributes you'd like to update. The second argument is where the error/success callbacks are accessed.

Preferably you use song.set({ hash attributes }) and later call .save with an empty hash as the first argument.