Comments (13)
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.
+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.
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.
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.
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.
Sorry, I'm being a bit slow. Can't you load records in using Model.fetch() ?
from spine.
Fetching blows any data already in memory away.
from spine.
Ok, how about we make fetch() append data by default, with an optional option to do a full refresh?
from spine.
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.
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.
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.
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.
doing Model.first().ajax().reload()
and Model.fetch({id : 1})
seem perfectly allright to me :)
from spine.
Related Issues (20)
- code help( help with the completion of the code to run well) HOT 3
- testing new issue HOT 5
- Why there are two different spinejs? HOT 4
- Do not mix up instance property and local variable HOT 5
- Broken link to coffeescript in README
- Crashes if the browser doesn't support localStorage HOT 1
- Triggering refresh on model instance no longer triggers refresh on the model class HOT 9
- `isBlank` suppose to check the number? HOT 4
- Support multiple remote hosts (override Spine.Model.host) HOT 1
- 1.6 ajax create success and then auto ajax update HOT 9
- hasOne and hasMany calls inconsistent
- listeningTo the same event on different models is broken (events.split / cannot read property 'obj' of undefined / etc.) HOT 3
- Extend POST/DELETE fix from 1.6.0 to PUT calls HOT 1
- spinejs.com domain registration has expired HOT 3
- Model#changeID does not persist the new id HOT 1
- Documentation link in README.md is broken HOT 2
- Model foreign key reference with no id broken HOT 2
- Model.find unable to get correct result
- Hello. Is this project abandoned? HOT 2
- Pug support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spine.