marmelab / wishcard Goto Github PK
View Code? Open in Web Editor NEWInDesign-like WYSIWYG editor using Raphaël.js and Backbone
Home Page: http://h6d3gw.marmelab.com
InDesign-like WYSIWYG editor using Raphaël.js and Backbone
Home Page: http://h6d3gw.marmelab.com
In https://github.com/marmelab/wishcard/blob/master/js/views/ShapesView.js#L60, it's better to do this.shapes.isEmpty()
. _.isEmpty is built in Backbone's Collections, as well as ,any other useful functions. See http://documentcloud.github.io/backbone/#Collection-Underscore-Methods.
It allows chaining of methods and is a convention in the Backbone world.
In https://github.com/marmelab/wishcard/blob/master/js/views/ShapesView.js#L50 you use the promise returned by fetch
to attach a "success" callback with the done
method. But in https://github.com/marmelab/wishcard/blob/master/js/views/ShapesView.js#L201 you pass a callback to the Shapes#addRaphaelElement
method. And this callback use the "node-ism" callback(err, result), rarely seen in the front-end world (apart with async.js).
You'd better return the promise of the save
call from https://github.com/marmelab/wishcard/blob/master/js/collections/Shapes.js#L36, but replace this call by :
return this.save(data);
This way you would use the promise way in https://github.com/marmelab/wishcard/blob/master/js/views/ShapesView.js#L201 the same way you did at L50, with the done
and fail
promise callbacks.
using the mouse
Separate view, models and collections into separate js files, and use RequireJS to bundle it all together.
cf. http://addyosmani.github.io/backbone-fundamentals/#modular-development
cf proof of concept http://jsfiddle.net/NtKKZ/
In https://github.com/marmelab/wishcard/blob/master/js/views/ShapesView.js#L12, no need to use jQuery to target '#editor'
, the string '#editor'
is enough. Backbone will use jQuery to find it when creating the view.
In Backbone, routers are useful to listen to change in urls. Their instanciation is almost always followed by Backbone.history.start()
to start the listening process.
Here, there's no use of the urls, and the Router is used only as an application initializer to create the view. So no need to use a Backbone construct for an initializer, a plain old javascript object will do.
Further reading from the creator of Marionnette, a Backbone based application framework : http://lostechies.com/derickbailey/2012/02/06/3-stages-of-a-backbone-applications-startup/
In https://github.com/marmelab/wishcard/blob/master/js/collections/Shapes.js#L11 you use _.bindAll(this);
. It's pretty dangerous to use this, unless the collection's methods are going to be used as response callbacks to events triggered somewhere else, where the meaning of this
is lost. Try to test without the call to see if it works (I think it should), then remove it. It can cause very malicious bug very hard to track (I know, it happened to me yesterday, really).
No need to set a custom url property for Shape models in https://github.com/marmelab/wishcard/blob/master/js/models/Shape.js#L15, as long as you fetch a model from shapes/[id]
. A model uses its collection's url
property and append its id to it automatically for you. Furthermore, it's more restful to use shapes/[id]
than shape/[id]
if the collection's url is shapes
. See http://documentcloud.github.io/backbone/#Model-url.
If you really want to stick to a custom Model's url, you can just redefine the url property as function instead of using the initialize
function, like :
var Shape = Backbone.Model.extend({
defaults: {} // your defaults here,
url: function() {
return 'shape/' + this.get('id');
// Better to use the getter than directly tap into the model's properties.
// Getters/setters trigger events that maybe be listened somewhere else in your app.
}
});
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.