在ember中,我们有动作助手;
drop table books;
drop table publishers;
create table publishers (
pubID int primary key not null identity (200,1),
pubName varchar(100) not null,
pubAddress varchar(200) not null,
pubPhone varchar(20) not null,
);
create table books (
bookID int primary key not null identity (1,1),
booksPubID int null constraint fkPubID foreign key references publishers(pubID) on update cascade on delete cascade,
bookTitle varchar(50) not null
);
insert into publishers
(pubName, pubAddress, pubPhone)
values
('World Class Books','9152 Longfellow Court Grand Island, NE 68801','(202)-555-0199'),
('Andy And Ally','19 Indian Summer Street Hempstead, NY 11550','(426)-535-4861'),
('Penguin Random House','7506 Fifth Court Holyoke, MA 01040','(202)-555-0148'),
('Hachette Livre ','58 Corona Ave. Kaukauna, WI 54130','(339)237-1422'),
('HarperCollins','9292 E. Cardinal Street West Lafayette, IN 47906','(361)474-5931'),
('Pan Macmillan','7002 Hillcrest Street Vernon Rockville, CT 06066','(713)515-6501'),
('Bloomsbury','9332 Ohio Ave. Copperas Cove, TX 76522','(219)738-8246'),
('Simon & Schuste','8464 Jockey Hollow Street Goldsboro, NC 27530','(610)921-2491'),
('Scholastic','47 West Heritage St. Henderson, KY 42420','(609)354-2678')
;
insert into books
(bookTitle)
values
('The Lost Tribe'),
('Misery'),
('The Silmarillion'),
('The Hobbit'),
('The Fellowship of the Ring'),
('Two Towers'),
('Return of the King'),
('One Fish Two Fish Red Fish Blue Fish'),
('The Cat in the Hat'),
('Eragon'),
('The Megicians Nephew'),
('The Name Of The WInd'),
('A Game of Thrones'),
('Mistborn'),
('2001 A Space Odyssey'),
('The FOrever War'),
('Hyperion'),
('Beyond the High Mist'),
('To Kill a Mockingbird'),
('The Giver'),
('Gathering Blue'),
('Messenger'),
('Son'),
('The Great Gatsby'),
('Pride and Prejudice')
;
select * from books
select * from publishers
但是,当我想将动作发送到父组件时,我仍然必须在调用sendAction的子代上显式写一个动作。即使我只是冒泡一个动作。我希望通过发送帮助来避免这种情况;
{{action "actionName" arg1 arg2}}
有谁知道我是如何实施这个助手的?
答案 0 :(得分:4)
您应该使用关闭操作,如Triggering Changes with Actions。
所示因此,如果您在名为actionKey
的父组件中有操作,则执行以下操作。
将其作为闭包动作传递给子组件:
{{my-component myName=(action 'actionKey')}}
然后在您孩子组件的模板中:
<button {{action myName arg1 arg2}}>click me</button>
我将myName
用于说明目的,只要它与您在组件中使用的名称相匹配,它就可以是您想要的任何内容。
我在Twiddle中复制了上述内容:https://ember-twiddle.com/fd56b0b9d017968fc334b3f109760806。