2

编写一个简单的日历脚本,尝试将事件发布到正确的日期;但是 jQuery$.extend并没有扩展我的options对象。以下是 HTML 文件的片段:

$("#cal").calendar({
    month: 2,
    events: [{
        date: "2013-4-10",
        workorder: 1234567890,
        district: 01,
        range: "2:00pm - 4:00pm",
        appNotes: "This is just a sample of some notes... end",
        installer: "John Doe",
        cityNotes: "This is just another sample of some notes... end"
    }, {
        date: "2013-4-1",
        workorder: 1234567890,
        district: 01,
        range: "3:00pm - 5:00pm",
        appNotes: "This is just a sample of some notes... end",
        installer: "John Doe",
        cityNotes: "This is just another sample of some notes... end"
    }]
});

我的插件的开头:

(function($){
    $.fn.calendar = function(options){
        var defaults= {
            month: null,
            prev: "prevMonth",
            curr: "currMonth",
            next: "nextMonth",
            events: []
        }, options = $.extend(defaults, options);
        return($(this).each(function(){
            var obj = $(this),
            // Etc...

我能够访问对象的属性,但是它并没有像想象的那样被扩展。有任何想法吗?

4

2 回答 2

3

jQuery.extend扩展传入的第一个对象 - 而不是做$.extend(defaults, options)do $.extend({}, defaults, options)

于 2013-04-05T01:01:08.807 回答
1
(function($){
    $.fn.calendar = function(options){
        var defaults= {
            month: null,
            prev: "prevMonth",
            curr: "currMonth",
            next: "nextMonth",
            events: []
        };
        options = $.extend(defaults, options);

另外:请记住,目标是第一个对象......所以在这个例子中,默认值将通过选项扩展......

于 2013-04-05T01:04:39.487 回答