K
K
Klaus Kater2015-05-06 17:10:42
Ember.js
Klaus Kater, 2015-05-06 17:10:42

How to access the Ember object, or how to save the object on ctrl+s?

Sobsno question describes the problem.
There is an event catcher, there are several tabs with object editors, in the handler you can find out which tab is active, in the tab there is information about the object (name, id, or something else, if desired) How to get an object from the click handler and save it?

document.body.addEventListener('keydown', function (e) {
   if ((e.keyCode === 83) && (e.ctrlKey)) {
      e.preventDefault();
      App.Tabs.tabs("option", "active"); //узнаем активный в данный момент таб
      console.log('ctrl + s');
   }
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Romanov, 2015-05-06
@Kaer_Morchen

The approach seems to be not very ember-way, you are almost isolated from the framework structure by the handler. You are not going to listen to the entire application, but only in some section.
You can do something like this in the view through the mixin:

App.SaveModelMixin = Ember.Mixin.create({
    onGlobalKeyDown: function() {
        Ember.$(document).on('keydown', {view: this}, this.globalKeyDown);
    }.on('didInsertElement'),

    offGlobalKeyDown: function () {
        Ember.$(document).off('keydown', this.globalKeyDown);
    }.on('willDestroyElement'),

    globalKeyDown: function (e) {
        if ((e.keyCode === 83) && (e.ctrlKey)) {
                e.preventDefault();
                App.Tabs.tabs("option", "active"); //узнаем активный в данный момент таб
                console.log('ctrl + s');
                // e.data.view - представление
                // e.data.view.get('controller') - контроллер, можно слать экшены
                // e.data.view.get('controller.model') - модель роута
        }
    }
});

I'm not sure though that this is a cool solution.

K
Klaus Kater, 2015-05-07
@kurojneko

On the stackoverflow, they advised such a weight github.com/satchmorun/ember-shortcuts, it seems to work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question