
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
can-interrupt
Advanced tools
Plugin for CanJS that allows you to interrupt the setting of a property (or properties) on can.Map in a transaction.
can-interrupt allows you to interrupt the setting of a property (or properties) on can.Map in a transaction. One use case of this is interrupting the setting of an AppState on route change when a user is attempting to leave a page with unsaved changes. By interrupting the setting of the route in a transaction, you can interrupt the application's state change, present the user with a confirmation prompt notifying them of their unsaved changes, and ask them if they want to proceed and lose their changes, or stay on the page and save their changes. In fact, Can Interrupt was designed with this use case in mind. A few examples will illustrate the use of Can Interrupt.
Setting up a can.Map to use Can Interrupt involves the following steps:
changing event:
myCanMap.bind("changing", function (event) {
//See examples below for working with the event object methods
});
The work you will do when Can Interrupt interrupts the setting of the can.Map will happen here. The event object passed
in to the callback function contains pause, resume, and cancel methods you can use to manage the transaction.
can.transaction.start();
recipe.attr('level', 'easy');
recipe.attr('name', 'blah');
recipe.attr('type', 'cream');
can.transaction.stop();
If you are using Can Interrupt with can.route, you do not need this step. All you need to do is bind the route to the
changing event, as described in step 1, above. Can Interrupt will manage wrapping route changes in a transaction for you.
//Define a Recipe can.Map
var Recipe = can.Map.extend({
define: {
name: {
value: 'cheese'
},
level: {
value: 'hard'
},
type: {
value: 'dairy'
}
}
});
//Create an instance of the Recipe can.Map
var recipe = new Recipe();
recipe.bind("changing", function (event) {
var mapProperty = event.args[0];
if (mapProperty === 'name') {
event.cancel();
}
});
//Open a transaction
can.transaction.start();
//Change properties on the recipe instance
recipe.attr('level', 'easy');
recipe.removeAttr('name');
recipe.attr('type', 'cream');
//End the transaction
can.transaction.stop();
//The properties are unchanged
//recipe.attr('name') --> 'cheese';
//recipe.attr('level') --> 'hard';
//recipe.attr('type') --> 'dairy';
can.route.bind("changing", function (event) {
if(hasUnsavedChanges) {
event.pause(function () {
//Add the current path to the browser history
var path = can.route.param(can.route.data.serialize(), true);
can.route._call("setURL", path, []);
//Present the user with a confirmation dialog
showConfirmationDialog({
text: 'Leaving the page will cause you to lose your unsaved changes. Would you like to continue?',
})
//The user has chosen to stay on the page, cancel the transaction
.then(function cancel (d) {
event.cancel();
},
//The user has chosen to leave the page, resume the transaction
function proceed (d) {
event.resume();
});
});
} else {
//There are no unsaved changes, resume the transaction
event.resume();
}
});
Can Interrupt provides the following methods:
can.transaction.start Used to begin a transaction set. Once you have called the start method, all changes to watched can.Maps will be tracked.can.transaction.stop Used to end a transaction set. Once you call the stop method, the changes to watched can.Maps will either be committed or ignored and no further changes will be tracked.When you bind a can.Map, or can.route to the changing event, your event handler will receive an event object. This object has the following methods:
pause Used to pause the setting of the values of the can.Map until either the resume or cancel methods have been called.resume Used to resume the committing of changes to the watched can.Map (this will cause your changes to the map to be saved).cancel Used to ignore (or roll back) changes to the watched can.Map (this will prohibit your changes to the map from being saved).FAQs
Plugin for CanJS that allows you to interrupt the setting of a property (or properties) on can.Map in a transaction.
We found that can-interrupt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.