GithubHelp home page GithubHelp logo

amccloud / backbone-tastypie Goto Github PK

View Code? Open in Web Editor NEW
99.0 99.0 22.0 274 KB

Modifications to Backbone's Model an Collection so that they play nice with django-tastypie. Includes a way to easily paginate over a resource list and a way to access the associated meta information.

License: MIT License

JavaScript 100.00%

backbone-tastypie's People

Contributors

amccloud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

backbone-tastypie's Issues

filters in a Backbone.Collection

I've setup two separate collections on a page and setting up the filters for one (passed as an arg to the constructor) causes the filters on the other to be set, because they both reference the original filters variable on the Backbone.Collection.prototype object.

I solved this issue by using the following code locally:

_.extend(Backbone.Collection.prototype, {
        initialize: function(collections, options) {
            _.bindAll(this, 'fetchNext', 'fetchPrevious');

            this.meta = {};

            this.filters = {
                limit: Backbone.Tastypie.defaultLimit,
                offset: 0
            };

            if (options && options.filters)
                _.extend(this.filters, options.filters);
        },

Basically I changed this.filters to be a variable that is declared inside the constructor and bound to the collection instance; this solved my problem.

Merging pagination into the other backbone-tastypie

Hi,

I've found your project a couple days ago when I was looking into doing pagination with backbone + tastypie. Thanks for releasing it :)

I'm not sure if you know the other backbone-tastypie project. But I think the best would be to merge your pagination into that one. It's a bigger project that includes features not supported in this one.

Right now I'm using both together, importing this library first, then the other backbone-tastypie and then changing collection.url there to do url + this._getQueryString(), but the integration feels quite hacky.

What are your thoughts? Cheers
Miguel

reset() function not resetting limit and offset

I believe that calling the reset function on a collection should reset the limit and offset of the current collection; it doesn't do this at the moment.

Easy patch code that I have written myself at the moment:

Inside the IIFE for this plugin:

var originalResetFunction = Backbone.Collection.prototype.reset;

Inside the extend that gives the Backbone.Collection the extra methods:

       reset: function (models, options){
            originalResetFunction.apply(this, arguments);
            this.filters = {
                limit: Backbone.Tastypie.defaultLimit,
                offset: 0
            };
            return this;
        },

I despise the cheap hack that I had to use but I'm not sure on what the correct way would be of calling the original Backbone.Collection.prototype.reset function would be; the issue is that because we have extended the prototype itself at compile time when it comes to run-time the prototype reset function is already pointing to the new reset function.

It would be good if this feature could get implemented and also a better solution could be presented; I'm interested in finding out what the correct way of doing this would be.

Support for fetching arbitrary pages

Could you add support to handle fetching arbitrary pages, e.g. fetching page 3 when you've only actually fetched the first page so far; thus skipping page 2 entirely?

fetching individual models

I came across a case where I wanted to fetch an individual model given a certain id, the quickest and easiest way I thought of doing this would be assigned a new model a given id and then fetching it from the server. I changed the code to allow for this:

I added the following in the url function for the extended Backbone.Model.prototype:

        if (this.get('id') !== undefined)
                return url + this.get('id') + '/';

I'm not sure if people have come across this kind of thing before and would be interested in knowing if it should be done in a different way perhaps?

is argument needed for fetchNext() ?

Is not the fetchNext method expecting one parameter? (as it add options to expected hash)

from the README:

// Fetch and add next 20 users.
activeUsers.fetchNext();

I get the following error:

mycollection.fetchNext()
TypeError: Cannot set property 'add' of undefined

instead

mycollection.fetchNext({})
Object

collection.meta is static and shared among every collection created

I am handling different types of collections in one GUI and realized that collection.meta is shared in every collection instance I am handling.

Here you have a test that shows the issue :

Foo = Backbone.Collection.extend();
Bar = Backbone.Collection.extend();
foo = new Foo();
bar = new Bar();
foo.meta.offset = 12;

Now bar.meta.offset is 12, side effect.

Cheers,
Javier

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.