Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

3.1.12 Requirements::javascriptTemplate / pass vars from DataObject


Go to End


4 Posts   1098 Views

Avatar
timo

Community Member, 47 Posts

29 May 2015 at 6:55pm

Edited: 29/05/2015 7:05pm

In my Dataobject i am trying to pass variables with Requirements::javascriptTemplate ...

class Item extends DataObject {
.........
public function getCMSFields() { 
	
  		$fields = parent::getCMSFields();
		Requirements::javascriptTemplate('mysite/javascript/Item.js', array('Title' => $this->Title));

..........

As i expected, editing my Item- DataObject within a GridField should always pass $this->Title.
But doesnt.
In Item.js i call alert('$Title')
but
$this->Title always has a Value of the very first-called Item.
How can i force updating the variable?
thanks.timo.

Avatar
Devlin

Community Member, 344 Posts

29 May 2015 at 7:21pm

I think you need to set the $uniquenessID parameter to require the file more than once.

Requirements::javascriptTemplate(
	'mysite/javascript/Item.js', 
	array('Title' => $this->Title),
	'mysite-javascript-Item-' . $this->ID
);

Though, I don't know if this works. Never tried. Maybe it would be better to require the javascript in the init() method and add a event listener for whatever you're trying to do in getCMSFields()...

Avatar
timo

Community Member, 47 Posts

29 May 2015 at 7:50pm

Edited: 29/05/2015 7:52pm

Hi Devlin!
ive tried your suggestion but
as i need to access the javascript in cms the init- function doesnt work. also the unique part didnt do it.
found this:
http://www.silverstripe.org/community/forums/general-questions/show/49384
or do i miss something?
any further ideas?
t.

Avatar
Devlin

Community Member, 344 Posts

29 May 2015 at 9:30pm

Edited: 29/05/2015 9:40pm

I found this: http://takeaway.bigfork.co.uk/a-beginners-introduction-to-using-entwine-in-silverstripe
Also: http://docs.silverstripe.org/en/developer_guides/customising_the_admin_interface/javascript_development/

Maybe something like this -- js only though

public function getCMSFields() {
	$fields = parent::getCMSFields();

	Requirements::customScript("
		(function($) {
			$.entwine(function($) {
				// find Title input field
				$('#Form_ItemEditForm_Title').entwine({
					onmatch: function() {
						// alert(this.val());
						console.log(this.val());
					}
				});
			});
		})(jQuery);
	");

	return $fields;
}

Good luck.