Search⌘ K
AI Features

Model Defaults and Events

Explore how to define default values in Marionette models to avoid missing data issues and apply event handling in views. Understand managing click events to trigger alerts and manipulate model data dynamically within your application.

Using model defaults

What do we do if our contact doesn't have a first name? We don’t want our app to break if the firstName attribute is missing since the template would be trying to retrieve an attribute that doesn’t exist on the model.

How can we manage this case? The functionality we’re looking for are default values for model attributes.

To declare default attribute values, simply add a defaults object to the main object provided in our model definition:

Node.js
ContactManager.Contact = Backbone.Model.extend({
defaults: {
firstName: ""
}
});

We declare the following model instance: ...