benpickles / js-model Goto Github PK
View Code? Open in Web Editor NEWWork with models in your JavaScript
Home Page: http://benpickles.github.io/js-model/
License: MIT License
Work with models in your JavaScript
Home Page: http://benpickles.github.io/js-model/
License: MIT License
...would be useful. In the case of embedding widgets in other sites where this library could potentially be loaded already it would be nice if version could be detected. It's a good practice for potentially common libraries. In jQuery and Underscore there's also a noConflict() method, but knowing the version would at least make it possible to detect if versions match. Usually VERSION constant within the namespace is a practice: Model.VERSION. What u say? :)
model_class_methods.js was heavily updated and removed the all
method along with many others in b09e2df. It looks like now we are to rely on a functional programming method for interacting with objects of the collection. Is that the case and can you give any details about what this move is all about?
Hi,
I'm getting a weird issue with doing model.save() on a model which has restful persistence. When I call model.save(), it is not passing any params to the restful persistence path at all.
I've checked that attributes are set for the model, as well as the path passed to the Model.REST argument, they are both fine. Project.load() works fine in getting the collection of objects. I double-checked that there was nothing wrong with the json API being access by js-model by testing a POST request with RestClient in Ruby, and that worked fine. I'm at a loss; the problem seems to lie with js-model passing params when doing a post request.
This is on version 0.9.3 (minified) with jQuery 1.4.4 (minified).
I have a private repo where this is occurring. Let me know if you want access to it for debugging.
Regards,
If I use this.persistence(Model.localStorage);
the Chrome freezes to the pint I have to restart it.
Any toughts?
After some rumbling I got js-model to work with cross-domain scripting - which is vey much a requirement for most web apps today. Not complete, but here's how the AJAX call should look like:
return jQuery.ajax({ type: method, url: url + '.json', dataType: "jsonp", jsonpCallback: 'JSONP', data: data, dataFilter: function(data, type) { return /\S/.test(data) ? data : null; }, success: function(data) { self.xhrComplete({status: 200, responseText: data}, 'success', model, callback) }, error: function(xhr, textStatus) { console.error(xhr) } });
As you can see I'm trying to fake a XHR-object here because XHR is not really used for JSONP-requests under the hood (using script tags hack), and "complete" callback don't appears to be triggered by jQuery for JSONP-requests. I'm hardcoding 200 status here which is a bit ugly I guess, but as I understand it JSONP-requests work a status code is not available - just error/success.
So personally I need this hack for my own and I bet plenty of js-model users will end up needing it as well. Is it of interest?
Hello,
I'm having some problem at destroying models (called Lines). After calling the destroy method and refreshing the page, the load() method will still bring those same models to the list again:
Both Chrome and Firefox have the same behavior. Am I missing something about the API?
Thank you!
can this be used with underscore.js?
Hi, i'm with a little probleman on method add in the model
look this gist, put all things that i`ve done and the error message
I use express js as backend, you could read several issues with application/json with no body. It's returns invalid json
Hi Ben,
thank your for the plugin!
Do you have a recommendation concerning nested models with arrays?
It seems, that
var post = new Post({ foo: ["bar","baz"] })
works well until I save it via REST - rails moans about the array instead of a hash.
Is it a bad idea overall, or do you think it would be possible to tweak rails' params-parsing or write an other persistence strategy?
-- Frank
Using jquery 1.3.1 when running the validation example in the read me post.valid() returns false when all the validations are met. Is there a requirement for jquery 1.4+?
If you use $ instead of jQuery in model_rest you can use other libraries than jQuery such as tire.js and zepto.js.
another possibility is to have an adapter incase those libraries are missing a feature it needs.
You should consider putting this library on Bower.
I install Sammy.js from Bower and it looks like there is a lot of talk online about using these two libraries together. It would be great if they were installable the same way.
Hello.
How can i remove an entire model collection when using local storage.
Thanks
By example:
project.bind("change", function() { ... });
project.attr({ name: "Hello" }); /* triggers 'change' */
project.attr("name", "Hello"); /* doesn't trigger 'change' */
It seems like this is by design. Can I ask what's the rationale behind that?
When will you release the next version?
Any future plans?
I really like the idea behind js-model and I would like to use it in my next project, but I can't use it if there is no further developement.
Especially I need to do some updates offline in localStorage and sync them with a REST api as soon as reconnected to the internet.
Hi Ben,
Thanks for sharing js-model, it's very nice to work with.
I am facing an issue however that might look simular to this one: #16, though I am not completely sure.
When I update the attributes of an existing model and call save afterward a put request is sent to the server. All is well but it seems like the request is not properly encoded. When I open up my Chrome developer tools and take a closer look in the headers section of the network tab, I can see that the data is sent as "request payload" rather than the expected "form data". Also this post (http://stackoverflow.com/questions/10494574/what-is-the-difference-between-form-data-and-request-payload) pushed me somewhat in the right direction, although I could not find the exact solution.
Regards,
Maarten
var Project = new Model('project', function(){ this.persistence(Model.REST, "api/projects"); });
var project = new Project({name:"testing12345"});
project.save();
It will send data like this : {project: {name: "testing12345"} }
yet my backend expect just attributes {name:"testing12345"} . Can this be configured in js-model, or do I have to change my API ?
thx
I was playing with js-model over the weekend and dates in models don't work as I expected they would. Objects with properties set as dates get serialised to strings and the when the object is parsed from localstorage it returns a string.
I had a quick look into the problem and it seems like a custom replacer callback for the JSON.parse and JSON.stringify is needed to fix this.
Is this project being maintained??
If you have a large collection of Projects and run something like,
Project.each(function(){ Project.remove(this)});
you'll find that
Project.count()
is not zero. I'm guessing there is concurrent activity between the index location and then splicing in Model.remove.
Maybe the collection could be made an object keyed on id: or some other primary key attribute to fix this problem.
Any plans for rudimentary associations between models? How are you doing this now?
Hi Ben,
Just noticed a problem when using with Rails.
Rails destroy actions return an empty string by default meaning the 'ajaxError' event gets triggered. This may well happen with updates too but I've not tested.
I'm not sure of the best way round this as there is already a ticket for Rails which they're not going to fix: https://rails.lighthouseapp.com/projects/8994/tickets/5199-respond_with-returns-on-put-and-delete-verb
What are your thoughts on this?
Cheers,
Jamie
Hi,
This is very minor. But, I was wondering if you have considered using on
/off
for bind
/unbind
following the preferred convention in jQuery or some other libs.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.