How do I deprecate a feature in my addon? - ember.js

I am writing an Ember.js Addon, and I need to deprecate a feature. Can I use the same deprecation system that Ember uses?
I want my users to be able to silence the warnings.
Ideally, I also need to be able to test that the deprecation message works under the right conditions, from within my test suite.
I'm using Ember 3.x.

An addon can use the same style of deprecation warnings as Ember uses, through the deprecate method:
import { deprecate } from '#ember/application/deprecations';
deprecate takes three arguments: the string as a message, a boolean test where falsey means the deprecation will be shown, and an options object with more information to be displayed.
For example:
import { deprecate } from '#ember/application/deprecations';
// ...
deprecate(
"message about your addon that mentions the addon by name",
false,
{
id: 'send-component-hash',
until: '2.0.0',
url: 'some url goes here'
}
);
// ...
The message in the console will look something like this:
Testing a deprecation can be done with a library like ember-qunit-assert. Once it is installed, expectDeprecation will be available on assert if you are already using qunit in your tests:
assert.expectDeprecation(/expected deprecation message/);

Related

How to enable ds-rollback-attribute in ember?

Ember has method rollbackAttribute() which is very similar to the default method rollbackAttributes(). The difference is rollbackAttribute() can be used to rollback ONLY specific model attribute.
By default this method is not available and to use it you need to enable ds-rollback-attribute and run the canary build as written here: https://docs.w3cub.com/ember/classes/ds.model/methods/#rollbackAttribute
Where can I enable the ds-rollback-attribute and how can I ran the canary build?
I fear you are looking at non-official and outdated API docs. The API docs for Ember Data are hosted here: https://api.emberjs.com/ember-data/release
The rollbackAttribute() method is not documented anymore for the latest release, which is 3.13 at the time writing this. It was last documented for 3.1. I think it was removed as a stale feature flag in this PR: [FEAT] remove all stale feature flags #5384
Actually the implementation of the rollbackAttribute() is quite simple.
We can create our own method and extract it into the service.
app/services/rollback-attribute.js
import Ember from 'ember';
export default Ember.Service.extend({
rollback(model, attribute) {
const changedAttributes = model.changedAttributes();
if (changedAttributes[attribute]) {
model.set(attribute, changedAttributes[attribute][0]);
}
}
});
After creating this service you can use it for example in the route.js
import Ember from 'ember';
import service from 'ember-service/inject';
export default Ember.Route.extend({
rollbackAttribute: service('rollback-attribute'),
_rollbackAttribute(model, attribute) {
this.get('rollbackAttribute').rollback(model, key);
},
});

Trouble upgrading to Ember/Ember-Data 2.0

I have an ember-cli project, and I'm trying to get to ember 2.0. I know the idea is to remove all deprecation warning before upgrading, but I don't know what to do about this one:
DEPRECATION: The default behavior of shouldReloadAll will change in Ember Data 2.0
to always return false when there is at least one "foo" record in the store.
If you would like to preserve the current behavior please override shouldReloadAll
in your adapter:application and return true.
[deprecation id: ds.adapter.should-reload-all-default-behavior]
This warning is related to a call that I make for this.store.findAll('foo') for example.
As far as I can understand, fixing this would involve change the behavior of either ember-data or ember-django-adapter.
Here is my (partial) package.json:
{
"name": "my-app",
"private": true,
"devDependencies": {
"ember-cli": "1.13.13",
"ember-data": "1.13.15",
"ember-django-adapter": "^1.1.1",
}
}
Here is a some of my bower.json:
{
"name": "my-app",
"dependencies": {
"ember": "1.13.11",
"ember-data": "1.13.15",
"ember-resolver": "~0.1.20",
}
}
So, after reading a bit, I thought maybe I can just ignore this warning, and maybe the behavior of shouldReloadAll isn't all that important for my app.
I'll list my steps carefully, because I'm not very familiar with npm or bower, and I may well be doing something wrong.
Change ember and ember-data to "~2.0.0" wherever they occur in package.json and ember.json
npm uninstall ember-data
bower uninstall ember-data
bower uninstall ember
npm cache clear
bower cache clear
npm install
bower install
At this point it is telling me that I have installed ember-data#2.0.1 and ember#2.0.2
I then run the application, and find the following error:
TypeError: str.replace is not a function
at Object.func (ember.debug.js:35278)
at Object.Cache.get (ember.debug.js:12867)
at decamelize (ember.debug.js:35320)
at Object.func (ember.debug.js:35235)
at Object.Cache.get (ember.debug.js:12867)
at Object.dasherize (ember.debug.js:35324)
at ember$data$lib$system$normalize$model$name$$normalizeModelName (normalize-model-name.js:13)
at ember$data$lib$serializers$json$serializer$$default.extend.modelNameFromPayloadKey (rest-serializer.js:426)
at ember$data$lib$serializers$json$serializer$$default.extend._normalizePolymorphicRecord (rest-serializer.js:161)
at rest-serializer.js:141onerrorDefault # ember.debug.js:29661exports.default.trigger # ember.debug.js:51067(anonymous function) # ember.debug.js:52059Queue.invoke # ember.debug.js:978Queue.flush # ember.debug.js:1042DeferredActionQueues.flush # ember.debug.js:838Backburner.end # ember.debug.js:166Backburner.run # ember.debug.js:288run # ember.debug.js:19125hash.success # rest-adapter.js:735fire # jquery.js:3148self.fireWith # jquery.js:3260done # jquery.js:9314callback # jquery.js:9718
The following versions are reported:
DEBUG: -------------------------------
vendor.js:15777 DEBUG: Ember : 2.0.2
vendor.js:15777 DEBUG: Ember Data : 2.0.1
vendor.js:15777 DEBUG: jQuery : 1.11.3
vendor.js:15777 DEBUG: -------------------------------
vendor.js:19380 Rendering application with bracketfun-web#view:toplevel: Object
PLEASE NOTE: This error doesn't seem related to the deprecation, in any explanation of the deprecation that I've received.
You need to create your application adapter by using ember generate drf-adapter application from the command line. This should create the file app/adapters/application.js, to which you will add the override of shouldReloadAll(), and the end result should look something like this:
import DRFAdapter from './drf';
export default DRFAdapter.extend({
shouldReloadAll() {
return true;
}
});
EDIT: replace error.
If you look at the stack trace and see where the error is coming from, you'll see it's originating from the serializer. It's struggling to get the model name from the payload. This is most likely due to the fact that you have not set up your application serializer.
You can generate the serializer using ember generate drf-serializer application. This will create the file app/serializers/application.js.
Remember Ember does not know to implicitly use an adapter / serializer by simply installing the add on, you need to tell it to use them by creating an application adapter and serializer.

Ember >2.2.0 getting regeneratorRuntime is not defined

so I was working with an iterator inside a service with Ember. The code worked using the old style scripts I cannot use the ES2015 style
ReferenceError: regeneratorRuntime is not defined
stuff[Symbol.iterator] = function *(){
debugger;
let properties = Object.keys(this);
for(let p of properties){
yield this[p];
}
};
I know this is because of the new '*' operator on the function. I have seen answers https://stackoverflow.com/a/28978619/24862 that describe having to load a browser-polyfill npm but I'm a little unclear how to get this to work inside the ember framework. Has anyone done this successfully? or should I just abandon until Ember supports it.
Polyfill
Babel comes with a polyfill that includes a custom regenerator runtime and core-js. Many transformations will work without it, but for full support you may need to include the polyfill in your app.
You should now include as ember-cli-babel and not as babel. Like this:
var app = new EmberApp(defaults, {
'ember-cli-babel': {
includePolyfill: true
}
}
Regenerator:
This package implements a fully-functional source transformation that takes the syntax for generators/yield from ECMAScript 2015 or ES2015 and Asynchronous Iteration proposal and spits out efficient JS-of-today (ES5) that behaves the same way.
Sources: https://github.com/babel/ember-cli-babel and https://github.com/facebook/regenerator
Perhaps your use of Babel.js needs to include the polyfill, in your ember-cli-build.js file use:
var app = new EmberApp(defaults, {
// Add options here
babel: {
includePolyfill: true
}
});

Why does this Ember.js app work locally but not on jsFiddle.net?

Here's the fiddle. Here's the gist with the contents of my local file.
As you can see, the HTML and JavaScript are identical, and I'm loading identical versions of the jQuery, Handlebars.js, and Ember.js libraries. It works as expected locally, but does not render the application template on jsFiddle.net.
I see the following error in the Web Console:
[19:44:18.202] Error: assertion failed: You must pass at least an object and event name to Ember.addListener # https://github.com/downloads/emberjs/ember.js/ember-latest.js:51
BTW-To test the gist as a local HTML file, make sure to run it behind a web server or your browser won't download the JavaScript libs. If you have thin installed (ruby webserver), go to the directory it's in and run thin -A file start, then navigate to localhost:3000/jsfiddle-problem.html in your browser.
If you set the "Code Wrap" configuration on your fiddle to one of the options other than "onLoad" your application will work. Here is an example.
The reason for this is Ember initializes an application when the jQuery ready event fires (assuming you have not set Ember.Application.autoinit to false). With the jsFiddle "Code Wrap" configuration set to "onLoad" your application is introduced to the document after the jQuery ready event has fired, and consequently after Ember auto-initializes.
See the snippet below from ember-latest, taken on the day of this writing, which documents Ember auto-initialization being performed in a handler function passed to $().ready.
if (this.autoinit) {
var self = this;
this.$().ready(function() {
if (self.isDestroyed || self.isInitialized) return;
self.initialize();
});
}
This was strange - I couldn't get your fiddle working, specifically your {{controller.foo}} call until I disabled autoinit. I am guessing when using jsfiddle the application initialize kicks off before seeing your router. I also noticed with your fiddle the router was not logging any output even when you had enableLogging set to true.
I updated your fiddle to not use autoinit, http://jsfiddle.net/zS5uu/4/. I know a new version of ember-latest was released today, I wonder if anything about initialization changed.

how properly delete cookies?

I use Selenium Webdriver, I want clear cookies before execute test. I use code from official 'Selenium` site. This is code:
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);
Set<Cookie> allCookies = driver.manage().getCookies();
for (Cookie loadedCookie : allCookies) {
System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
}
driver.manage().deleteAllCookies();
But I get notification: - Cookie cannot be resolved to a type, Set cannot be resolved to a type
The Set is of java.util package.
The Cookie is of org.openqa.selenium package.
You need to import those two classes to make your code work:
import java.util.Set;
import org.openqa.selenium.Cookie;
To make the suffering less painful, every modern Java IDE has an automatic function for this:
In Eclipse, it's called "Organize imports" and sits under Ctrl+Shift+O.
In IntelliJ, it's called "Optimize imports" and sits under Ctrl+Alt+O.
In NetBeans, it's also called somehow and sits under Ctrl+Shift+I.
I'd check your imports. I suspect that you are using the javax cookie when you need the selenium one.
javax.servlet.http.Cookie
org.openqa.selenium.Cookie