Search code examples
backbone.jsgetmode

backbonejs's get method doesn't work


suppose I have a model:

window.PageModel = Backbone.Model.extend({

    default:{
        "device":"ipad",
    "lang":"zh-Hans",
    "page":2,
    "uuid":"3c30e128-ed44-43a7-960a-e937457b8b58"
    },

    initialize: function () {
        console.log('lang',this.get('lang'));
        console.log('device',this.get('device'));

    }
})

but when I initialize it,

window.pageModel = new PageModel();

the firebug tell me "lang" and "device" is undefined.

Why this happened? How can I solve this problem ?


Solution

  • you should be using
    defaults:{}
    instead of
    default:{}
    so your code will be

    defaults:{
            "device":"ipad",
        "lang":"zh-Hans",
        "page":2,
        "uuid":"3c30e128-ed44-43a7-960a-e937457b8b58"
        }