I have a Backbone.js application that allows users to create graphs based on large datasets. There are several options presented to the user to allow for graph customization. Some of the options that the user may click set several attributes on the model.
Occasionally we have to create some of these graphs programaticaly- through loading or importing graphs from other applications. I’m not sure whether to trigger click events to set those attributes or to set model attributes directly. On one hand, it’s DRY to trigger the click event, but I’m not sure if that’s efficient/readable or violates MVC.
Is it bad practice to simulate a click in order to set model attributes? Or should I set the attributes on the model manually?
CODE:
View
...
events: {
"click .defaultGraph": "setDefaultGraph"
}
, setDefaultGraph: function () {
this.model.set({
"type": "bar"
, "otherAttribute": "foo"
, "anotherAttribute": "baz"
});
}
...
My programmatic recreation of a default graph
this.$(".defaultGraph").click();
OR
this.model.set({
"type": "bar"
, "otherAttribute": "foo"
, "anotherAttribute": "baz"
});
It doesn’t matter. The
clickway is short and sweet. So, if the view isn’t going to change (which I know you can’t say with surety) thenclickway is fine. Otherwise, you may want to go for the latter way.