_selectors: function () { return { elemName: '.block__elem-name' }; }
We will add all the selectors in one place and give a clear name to the elements for the selection of which they are needed. By the way, we will choose them like this: this._elem('elemName');
instead this.$('.block__elem-name');
'div > tr.row[data-active=”true”] a.red-Button'
for “buy” buttons. this._selector('elemName');
This is also necessary. $('div > tr.row[data-active=”true”] a.red-Button').blahBlah();
and after 10 lines like this: $('div > tr.row[data-active=”true”] a.red-Button').anotherBlahBlah();
var $buyButton = $('div > tr.row[data-active=”true”] a.red-Button');
oh no, you have the same Backbone - bring it to the property this._$buyButton = this.$('div > tr.row[data-active=”true”] a.red-Button');
or have you already connected Backbone.View.Elements? this._$buyButton = this._elem('buyButton');
_elem
caches it all, so just this._elem('buyButton');
this._findElem('elemName');
searches without using cache this._dropElemCache('elemName');
cleans the cache for a particular item, and this._dropElemCache();
cleans your entire cache to shine when you realize that the time has come. For example, after rendering. this._$window; this._$body; this._$document;
$('div > tr.row[data-active=”true”] a.red-Button').css({color: 'magenta'});
Rather pepper everything with declarativeness and mix well with CSS: .button_active { color: magenta; }
We’ve taken care of class manipulation. First, we denote all classes in one place: _classes: function () { return { activeButton: 'button_active' }; }
And then you want - add a class this._addClass('activeButton', 'buyButton');
want - delete: this._removeClass('activeButton', 'buyButton');
want - switch: var condition = !!Math.round(Math.random()); this._toggleClass('activeButton', 'buyButton', condition);
this._selector('activeButton'); // returns '.button_active'
or you can search for items: this._elem('activeButton');
Just do not forget about the cache, because the active button probably changes this._findElem('activeButton');
var id = 5, state = 'highlighted'; $('.item[data-id=”' + id + '”]').addClass('item_state_' + state);
This is where complex selectors come into play: _classes: function () { return { itemInState: 'item_state_%s' }; }, _selectors: function () { return { itemById: '.item[data-id=%s]' }; }
Then the following will be true: this._class('itemInState', 'highlighted'); // 'item_state_highlighted' this._selector('itemInState', 'highlighted'); // '.item_state_highlighted' this._selector('itemById', 5); // '.item[data-id=5]'
var id = 5, state = 'highlighted'; this._addClass(['itemInState', state], ['itemById', id]);
The item_state_highlighted class will be added to the element found by the .item selector [data-id = 5] _classes: function () { return { item: 'item_%(mod)s_%(value)s' }; }
Each place has its own name this._elem('item', { mod: 'state', value: 'focused' });
Find a jQuery collection by '.item_state_focused'
this._data;
It stores the data of the root element of the view. So if you have a div
<div data-some-ids=”[5,6,7]”></div>
on which the view is initialized this._data['someIds']; // [5,6,7]
And if the data is stored in a specific element, then it will help you. this._getElemData('elemName', 'someIds');
To get all the data: this._getElemData('elemName'); // {someIds: [5,6,7]}
Source: https://habr.com/ru/post/249373/
All Articles