Models Flashcards

1
Q

How do you set an attribute on a model?

A

model.set(‘title’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you get an attribute on a model?

A

model.get(‘title’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you get a JSON representation of a model?

A

model.toJSON();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you clear all attributes on a model?

A

model.clear();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you unset an attribute on a model?

A

model.unset();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you see if a model has an attribute?

A

model.has(‘title’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to create a model?

A

var Model = Backbone.Model.extend();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you instantiate a model?

A

var model = new Model;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A
var Song = Backbone.Model.extend({
    defaults: {
        title: "default value"
    }
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you use validation on model attributes?

A
  • 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’;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you extend you custom models?

A

Animal.extend() just like extend Backbone.Model

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

Use the same method name and write new code.

17
Q

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

A

Javascript doesn’t provide an easy way, but you can do…

ClassName.prototype.methodyouwantocall.apply(this)

18
Q

What are the 3 persistence operators a backbone model has?

A

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

19
Q

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

A

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
Q

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

A

urlRoot: ‘/api/tasks’

21
Q

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

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

true/false Backbone routes are restful by default?

23
Q

how would you do a PUT request to the server?

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

song. set(‘Title’: ‘Hello world’);
song. save();

api/songs/1 PUT request;

24
Q

How would you create a new model and persist it?

A

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.