Code Monkey home page Code Monkey logo

Comments (13)

jasonroelofs avatar jasonroelofs commented on June 12, 2024

To expand on this, there's no way to directly pull information for a specific object via Ajax.

If I want to do /resources/14 I found I have to do the following:

Spine.Ajax.ajaxSync(Resource.init({id: $("#access_hash").val()}), "read");

And I had to patch spine.ajax.js (adding method === "read"):

if (method === "create" || method === "update" || method === "read") {

Where "#access_hash" is a hidden field including the id of the object I'm trying to find, rendered to the page.

from spine.

fwielstra avatar fwielstra commented on June 12, 2024

+1, I found it counter-intuitive to see Model.find() not working if it wasn't populated (and its implementation being a simple array lookup). It would be pleasant if this issue could be fixed, or instructions to how to get an ajax model to populate itself when you call find() with a specific ID.

Preferably sooner or later; we're trying to build a prototype of a neat application, and we'd have to switch to Backbone or some other JS library that does do this if I can't get it to work soon.

from spine.

csaunders avatar csaunders commented on June 12, 2024

I had the same issue and implemented this, I just haven't taken the time to put it together (or test it enough). Hope it helps a bit:
https://gist.github.com/1031521

from spine.

maccman avatar maccman commented on June 12, 2024

The trouble is, this goes against Spine's philosophy somewhat, in that you should have a non-blocking interface. i.e. you should load all data you'll need locally before accessing it. Additionally it shouldn't be assumed Ajax is being used, or even that data is remote. For now, I'd do something like this:

  Model.extend({
    findRemote: function(id, callback) {
      $.getJSON(this.url(id), function(res){
        callback(this.fromJSON(res))
      })
    }
  })

  Model.include({
    getStuff: function(cb){
      $.getJSON(this.url('stuff'), cb)
    }
  })

from spine.

jasonroelofs avatar jasonroelofs commented on June 12, 2024

While I agree with your statement, there is a bug in the library somewhere. It is currently impossible without hacking something to get the initial state of an existing model into Spine's memory store.

Even if you have all the information you need rendered to the page, you have to ensure Ajax is turned off on the Model before trying to load that information into javascript memory because otherwise spine.ajax.js will send a POST request to create a new resource with that information.

So you're stuck with either a hack like we've described above, or making sure that you explicitly do

function readyForAjax() {     
    Resource.extend(Spine.Model.Ajax);
}

and only calling that once you've loaded the information from the page and called either Resource.create() or Resource.save() to put the information in Spine's model memory and only there. Either way this feels like something Spine should be able to handle itself, and it's something every single real-world app will need to do.

from spine.

maccman avatar maccman commented on June 12, 2024

Sorry, I'm being a bit slow. Can't you load records in using Model.fetch() ?

from spine.

csaunders avatar csaunders commented on June 12, 2024

Fetching blows any data already in memory away.

from spine.

maccman avatar maccman commented on June 12, 2024

Ok, how about we make fetch() append data by default, with an optional option to do a full refresh?

from spine.

jasonroelofs avatar jasonroelofs commented on June 12, 2024

In my situation I really can't render all the required information to the page and load from there. I give the page the basic information for accessing the parent object, then need to query the server for everything related to that object for rendering to the page. Once I get that information then I can initialize the app and go to work but this is where Spine / Ajax get in the way a little bit, because I need to make sure that as the data is loading, Spine.Ajax doesn't erroneously think this is a new model and queue up a POST Ajax request for that information I just loaded when I fill up Spine's model memory.

It feels to me, and it may be a lack of understanding on my part, that Spine supports creating a new resource really well, but editing an existing resource is lacking somewhat. I need to get the data of the existing resource from the server before the app can start/continue working.

from spine.

jasonroelofs avatar jasonroelofs commented on June 12, 2024

Ok talking with Alex my issue was confusion and the sparse documentation of how to work with Spine.Ajax. With my urls set up right, and a bind on "refresh", Resource.fetch() will get the information I need and I'll know when it's loaded into memory.

from spine.

csaunders avatar csaunders commented on June 12, 2024

The current way spine fetch works (as I understand it) is you can pass in a single variable, this being a callback or options. I was thinking that this option might end up overloading the fetch function too much.

So instead there could be a separate function or you can pass in a hash that could contain a callback and/or options. Personally I like having the hash or a multi-argument function instead of a new one.

So yeah, I think passing in an option as to whether you would like to append or replace the current data when fetching would be great.

from spine.

maccman avatar maccman commented on June 12, 2024

replace() now takes an option to optionally blow away existing records. Also Spine.Ajax has be re-factored, which gives much more scope for custom calls and should address all the concerns above (see CS branch).

from spine.

maca avatar maca commented on June 12, 2024

doing Model.first().ajax().reload() and Model.fetch({id : 1}) seem perfectly allright to me :)

from spine.

Related Issues (20)

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.