Giter Club home page Giter Club logo

ember-pardon's Introduction

Ember Pardon

About

This Ember mixin is a Get Out of Jail Free Card that injects a 'beforeDestroy' hook and 'pardon'/'unpardon' methods which allow for the halt or continuation of an object's destruction.

Installation

  • npm install --save-dev ember-pardon

Usage

import Ember from 'ember';
import EmberPardon from 'ember-pardon';

// as a mixin:
var NewObject = Ember.Object.extend(EmberPardon,{
// ...
});

// or add to the original class:

Ember.View.reopen(EmberPardon);

Please Note: For Ember.View it is recommended to reopen the class because of how Ember organizes and manages virtual parent views. It is definitely possible to add as a mixin only, but keep in mind that Ember will destroy the view's virtual parent first. Ember Pardon checks its parent to see if it is virtual, and then checks to see if it can be pardoned. If so, then it pardons the parent, allowing the natural Ember view order to be maintained. Unpardoning the child does the inverse, and will attempt to unpardon the parent as well, but only if the parent is virtual.

Get Out of Jail Free

Ember Pardon gives your objects freedom once each cycle. It adds the following:

  • beforeDestroy() Called before willDestroy. Overriding this function and calling this.pardon() will save the object from being destroyed, which includes being removed from the DOM in the case of a View.

  • pardon() Retains the object from destruction during this one cycle.

  • unpardon() Reverts the object to default behavior, allowing it to be destroyed.

  • _isPardoned : Private

Private variable that tracks whether or not the object should be pardoned.

Complete Example

The following creates an example class that pardons the first time destroy is called, but will retain original behavior the second time around.

unpardon() only exists for those rare times that you want to cancel a previous pardon. PLEASE NOTE: Pardon and Unpardon are reset after each destroy cycle.

import Ember from 'ember';
import EmberPardon from 'ember-pardon';

Ember.View.reopen(EmberPardon);

var ExampleView = Ember.View.extend({
	shouldStickAround:true,
	beforeDestroy:function(){
		if (this.shouldStickAround){
			this.set('shouldStickAround', false);
			this.pardon();
			return;
		}
		
		/**
		unpardon used here doesn't really do anything
		because nothing was changed above.  Use this
		to cancel out previous calls to pardon().
		**/
		this.unpardon();
		
	}
});
export default ExampleView;

For more information on using ember-cli, visit http://www.ember-cli.com/.

ember-pardon's People

Contributors

jameslallen avatar kategengler avatar ember-tomster avatar

Watchers

 avatar

Forkers

kategengler

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.