Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set property 'items' of null #81

Open
myfashionhub opened this issue Oct 14, 2014 · 5 comments
Open

Cannot set property 'items' of null #81

myfashionhub opened this issue Oct 14, 2014 · 5 comments

Comments

@myfashionhub
Copy link

This error gave me a big headache $('#paginator').pagination('updateItems', numVideos). It's supposed to call this function below. Turns out o is null.

        updateItems: function (newItems) {
            var o = this.data('pagination');
            o.items = newItems;
            o.pages = Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
            this.data('pagination', o);
            methods._draw.call(this);
        },

After much investigation, I realize that the init method is called after updateItems, and that's why o, which is the options object, is null. I did a setTimeout on calling $('#paginator').pagination('updateItems', numVideos) and it seems to prevent the error. I'm not sure why this happens though.

@bilalakil
Copy link
Contributor

I'm not able to verify this until later, but I'm pretty sure the problem is simply that you're running a pagination function other than init (i.e. updateItems) before having run the init function yourself:

$(selector).pagination({
    items: 100,
    itemsOnPage: 10,
    cssStyle: 'light-theme'
});

Initialisation is explicit. We can't "automatically" init when you ask to updateItems because we don't know what init options you need.

Your code must always init an element as above BEFORE calling any other pagination functions on it. It worked for you with a timeout because your init executed some time after your updateItems, however a timeout should never be needed for this.

@myfashionhub
Copy link
Author

$(selector).pagination({
    items: 100,
    itemsOnPage: 10,
    cssStyle: 'light-theme'
});

I have tried adding that block before calling updateItems. It seems like it did call init function but not really. I still get the null error the first time activating updateItems (it works the second time).

@bilalakil
Copy link
Contributor

Be careful to check that you're actually calling it before updateItems, because when I tested it that's what made the difference:

http://jsfiddle.net/sge1830c/2/

An easy way to check is to put console.log()s just before the simplePagination calls to see which one is logged first.

I still think that it should be fine as long as you initialise before calling anything else. If you're still having the problem even after a console.log() check then please share more of your code here so we can diagnose and hopefully reproduce the issue.

@bilalakil
Copy link
Contributor

Hmm, I followed my own link above from my iPhone and it's always crashing. I'll investigate when I get home.

@myfashionhub
Copy link
Author

I tried your plunker and both init before and init after show the crash message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants