-1

I used to program in C++ but I'm trying to program a facebook bot in Javascript.

I have a function Options that creates option buttons. The buttons are programmed like this (1):

"buttons": [{
"type": "web_url",
"url": "https://www.messenger.com",
"title": "Use"
}]

But since I have lots of them I would like to create a function to create buttons. I've tried to create a Button function like that:

function PLButton(type, title, payload) {
    "type": type,
    "title": title,
    "payload": payload
}

And then substitute the code above (1) by this:

"buttons": [{PLButton("postback", "Drop", "PRESS_CANCEL")}]

But it doesn't work.

Blue
  • 22,608
  • 7
  • 62
  • 92
Laura
  • 5
  • 1
  • Return an object literal from the PLButton function? – gcampbell Aug 04 '16 at 19:51
  • And by the way in object literals the keys don't have to be quoted. `{ foo: 42 }` is equivalent to `{ "foo": 42 }` (but if you need the *variable* `foo` instead, from ES6 you can do `{ [foo]: 42 }`). – gcampbell Aug 04 '16 at 19:54

3 Answers3

-1

The object literal syntax and function body syntax are not interchangeable, but you can next an object literal within a function. If you want to construct and return an object, you need to combine your two attempts:

function PLButton(type, title, payload) {
  return {
    "type": type,
    "title": title,
    "payload": payload
  };
}

The object literal syntax ({x: 1}) does something akin to creating a map (a dict-mode object) which the function can then return. This sort of helper is pretty common, since an object is easier to extend later than a list of loose parameters.

ssube
  • 47,010
  • 7
  • 103
  • 140
-1
var initialButtonList = {"buttons": [{
"type": "web_url",
"url": "https://www.messenger.com",
"title": "Use"
}]}

function PLButton(type, title, payload) {
var newButton={//create newButton as object that holds key-value pairs
"type": type,
"title": title,
"payload": payload
};
return newButton;
}
initialButtonList.buttons.push(PLButton("postback", "Drop", "PRESS_CANCEL"));
//initialButtonList will now have the newly added button as well;
Meghana
  • 41
  • 4
  • 9
    Welcome to SO. Can you explain why/how your code resolves the problem instead of just posting code? – DVK Aug 04 '16 at 20:18
  • 1
    Could you explain it better? I'm having some trouble calling the function inside my Options function: function Options(sender, name1, name2) { let messageData = { "attachment": { "type": "template", "payload": { "template_type": "generic", "elements": [{ "title": name1, initialButtonList.buttons.push(PLButton("postback", "Drop", "PRESS_CANCEL")); How should I do it ? :/ Thanks for your attention – Laura Aug 05 '16 at 10:35
  • @DVK,@Laura The code works as follows: initialButtonList is an object with property called buttons. Buttons is the array of objects, with each object holding list of the properties : type, url, title. Whenever a new button(which is also an object) is added, it should be added to the Buttons, which is achieved by the following line " initialButtonList.buttons.push(PLButton("postback", "Drop", "PRESS_CANCEL"));" – Meghana Aug 06 '16 at 06:34
  • Now, the buttons(array of objects) can be created in 2 ways: 1. When the value is static var initialButtonList = {"buttons": [{ "type": "web_url", "url": "https://www.messenger.com", "title": "Use" }, { "type": "web_url1", "url": "https://www.messenger.com", "title": "Use1" }, { "type": "web_url2", "url": "https://www.messenger.com", "title": "Use2" }]} – Meghana Aug 06 '16 at 06:35
  • 2. When the values are dynamic: The code written in the initial solution would help in generating the buttons. @Laura, "initialButtonList.buttons.push(PLButton("postback", "Drop", "PRESS_CANCEL"));" The following line is to add the new button to the array of buttons. This particular line should be added after the attachment object initialization. Never add the dynamic part inside the static declaration. – Meghana Aug 06 '16 at 06:35
  • For more reference refer the following [link](http://stackoverflow.com/questions/9946506/adding-an-object-to-an-array-of-objects-with-splice). This would help you get more info on how to add objects to the array of objects. -Meghana – Meghana Aug 06 '16 at 06:35
-1

Solved!

I've just created a function like that:

function PLButton(tipo, titulo, accao){

var Butt = {type:tipo, title: titulo, payload: accao};

return Butt;

}

And incede the funcion Options :

"buttons": [PLButton("postback", "Use", "PRESS_USE_SCISOR"), PLButton("postback", "Drop", "PRESS_DROP_SCISOR")],    
Laura
  • 5
  • 1