I am new to Backbone, so can't understand what I am doing wrong. What I need is to override saveCompat (media-models.js line 310, WP 3.9.1). I am trying to do it same way I have overridden some media views
wp.media.model.Attachment = wp.media.model.Attachment.extend({
saveCompat: function( data, options ) {
some code here...
}
});
But it doesn't work for me. Native WordPress saveCompat is executed. At the same time the very same idea is perfectly working for wp.media.view.AttachmentCompat for example
wp.media.view.AttachmentCompat = wp.media.view.AttachmentCompat.extend({
save: function( event ) {
some code here...
}
});
Thanks in advance!
I figured it out. The correct way to extend it is:
_.extend( wp.media.model.Attachment.prototype, {
saveCompat: function( data, options ) {
some code here...
}
});
Using _.extend removes the ability to call the super class implementation of methods. The proper (or at least a working) way to do it is to use Backbone's extend method and then overwrite the prototype in wp.media.model.Attachment like this:
var MediaCreditAttachmentModel = wp.media.model.Attachment.extend( {
saveCompat: function( data, options ) {
...
}
} );
wp.media.model.Attachment.prototype = MediaCreditAttachmentModel.prototype;
Related
Im a Drupal themer and im struggling to modify a module in the way that I need. When something happens I need to run a very simple bit of JavaScript. Ive found the part of the module responsable and the following works:
if (something = something else) {
return array(
'#commands' => array(
// Hacky by works
ajax_command_append('body',
'<script>
alert("Custom Js");
</script>'),
),
);
}
else {
// Do something else
}
However it would be better to call a function that was somewhere else as other modules will want to call it. How can I call a function from another module from within this statement?
You can do it inline, like this
Edit
In my first answer I forgot to wrap the javascript in jQuery(document).ready(function() {})
module
if (something = something else) {
// Inline fixed
drupal_add_js("jQuery(document).ready(function() { alert('Script inline!'); });", "type" => "inline");
// From file
drupal_add_js("myscript.js", "file");
return array(
'#commands' => array(
),
);
}
else {
// Do something else
}
myscript.js
(function() {
alert('Script from file!');
});
// or with jQuery
(function($) {
// Do something with $
})(jQuery);
If you want the javascript code to be in on its own file, do it like that (taken from the examples)
Check out the examples in the documentation
More information on adding javascript to your modules/themes
Adding Javascript to your theme or module
Managing JavaScript in Drupal 7
Edit 2
You can call a javascript function from a module. I would do it like this :
Create a new module that just inject a javascript code. This module should have a lesser weight than any other module so it executes before any of them.
Then call it from any module using Drupal.settings.
Assuming that you created the module, you should save your javascript function into Drupal.settings
(function ($) {
Drupal.settings.exampleModule = {
myFunction : function () {
alert('My example function!');
}
};
}(jQuery));
This will inject that code into settings and will be available to any module which needs to access it.
Your module
if (something = something else) {
// Inline fixed
drupal_add_js("(function($) { Drupal.settings.exampleModule.myFunction(); })(jQuery);", "type" => "inline");
}
else {
// Do something else
}
That's how I would do it. Now, if you need to use the ajax framework you should do something like this (try it, I've never used this framework before, so I'm guessing)
return array(
'#commands' => array(
// Hacky by works
ajax_command_append('body', "(function($) { Drupal.settings.exampleModule.myFunction(); })(jQuery);"),
),
);
I am using CodeMirror and attempting to do some CSS styling to the autocomplete pop up. This is a bit difficult, because I need it to not go away when I go to inspect styles and stuff.
So I hunted for a way to do this. I found this code in show-hint.js
if (options.closeOnUnfocus !== false) {
var closingOnBlur;
cm.on("blur", this.onBlur = function () { closingOnBlur = setTimeout(function () { completion.close(); }, 100); });
cm.on("focus", this.onFocus = function () { clearTimeout(closingOnBlur); });
}
If I comment this out, then the autocomplete pop up does not go away when I click on other things; That's what I wanted. But I thought I would explore this more and try to determine what to do to toggle this on and off at will.
So I wanted to be able to set this closeOnUnfocus option on my own. That seemed simple enough.
I cannot find a way to do this, though. Exploring further I found an example on code mirror's website that demonstrates a way to setup the autocomplete system using the following code;
CodeMirror.commands.autocomplete = function(cm) {
CodeMirror.showHint(cm, CodeMirror.hint.anyword);
}
Exploring further, show-hint.js starts out with a function called showHint that has this signature;
CodeMirror.showHint = function (cm, getHints, options) {
// We want a single cursor position.
if (cm.somethingSelected()) return;
if (getHints == null) {
if (options && options.async) return;
else getHints = CodeMirror.hint.auto;
}
if (cm.state.completionActive) cm.state.completionActive.close();
var completion = cm.state.completionActive = new Completion(cm, getHints, options || {});
CodeMirror.signal(cm, "startCompletion", cm);
if (completion.options.async)
getHints(cm, function (hints) { completion.showHints(hints); }, completion.options);
else
return completion.showHints(getHints(cm, completion.options));
};
Okay, so it stands to reason that I could accomplish what I want by passing my option through here; like this...
CodeMirror.commands.autocomplete = function (cm) {
CodeMirror.showHint(cm, CodeMirror.hint.anyword, {
closeOnUnfocus: false
});
}
But this doesn't work - in fact, it seems that the options just don't get passed at all. If I do a console.log in the show-hint.js, the options are outright ignored. They never get through.
So how can I pass options through? I am very confused.
If you want to change the styles of of the hint menu, just use the provided CSS hooks. There is no need to mess around with the autocomplete handlers. e.g.:
.CodeMirror-hints {
background-color: red;
}
.CodeMirror-hint {
background-color: green;
}
.CodeMirror-hint-active {
background-color: blue;
color: yellow;
}
And here's a live Demo.
I've just started to use Codemirror (v4.1) and I've found the same problem. After checking show-hint.js contents it seems that documentation is not updated.
Try to write this when you want to get the suggestions:
CodeMirror.showHint({hint: CodeMirror.hint.deluge, completeSingle: false, closeOnUnfocus: true});
If you need to use the async mode of getting suggestions (it was my case), now you have to do this before previous snippet:
CodeMirror.hint.deluge.async = true;
Hope this helps!
You can pass the options like this :
CodeMirror.showHint(cm,CodeMirror.hint.anyword,{completeSingle: false,closeOnUnfocus:false});
You can write the code as follows:
editor.on("keyup",function(cm){
CodeMirror.showHint(cm,CodeMirror.hint.deluge,{completeSingle: false});
});
It's working for me.
I'm using Fishpigs Wordpress integration module in a Magento store. When I set it to use a custom Wordpress menu, which I've set up in Wordpress with some category hierarchies, it doesn't add any active states if you've clicked a link and are on an "active" page. After digging about, /app/code/community/Fishpig/Wordpress/Model/Menu/Item.php has the following:
public function isItemActive()
{
return false;
}
So it seems like they've just skipped this bit? Anyone any idea how to set active states here?
OK, this seems to do the job, bit of a workaround but hey!
public function isItemActive()
{
$currurl = Mage::helper('core/url')->getCurrentUrl();
$linkurl = $this->getUrl();
if(strstr($linkurl, $currurl)){
return true;
}
else {
return false;
}
}
Get the current url, get the blog url, if they match set active state to true. I then used a bit of jQuery to set states of parents to active as the above only sets the current link:
$('#nav li.nav-3 ul li.level1.active').parent().parent().addClass("active");
...where li.nav-3 is the parent blog link
Replace the isItemActive function with following code in /app/code/community/Fishpig/Wordpress/Model/Menu/Item.php. This is working for me.
public function isItemActive() {
$myblogUrl = Mage::helper('wordpress/abstract')->getBlogRoute();
$mycurrentUrl = preg_replace('/\?.*/', '', Mage::helper('core/url')->getCurrentUrl());
if (in_array($myblogUrl, explode("/", $mycurrentUrl))) {
return true;
} else {
return false;
}
}
The problem is just a simple miss.. Magento uses a "/" in all urls,$currentUrl never matches $currentUrl because of this. The correction is just to trim the "/" I know a late response, but thought it may help someone.
public function isItemActive()
{
$currentUrl = Mage::getUrl('*/*/*', array('_current' => true, '_use_rewrite' => true));
if (strpos($currentUrl, '?') !== false) {
$currentUrl = substr($currentUrl, 0, strpos($currentUrl, '?'));
}
return $currentUrl === rtrim($this->getUrl(), '/');
}
can we use standard 'InsertCpDialog$initialize()' in our javascript like this , so that i can call other function once it get initializes. I used like below code but it is not working. :(
Type.registerNamespace("Extensions");
Extensions.InsertCpDialog.prototype.initialize = function InsertCpDialog$initialize()
{
alert('hi inside insert');
var p = this.properties;
if(window.document.nameProp == "Name" || window.document.title == "Name") {
var browserName=navigator.appName; // Get the Browser Name
if(browserName=="Microsoft Internet Explorer") // For IE
{
alert('hi inside IE');
//window.onload=init(); // Call init function in IE
}
else
{
if (document.addEventListener) // For Firefox
{
alert('hi inside firefox');
//document.addEventListener("DOMContentLoaded", init(), false); // Call init function in Firefox
}
}
}
}
Original(standard) one is like:
Type.registerNamespace ("Tridion.Cme.Views");
Tridion.Cme.Views.InsertCpDialog = function InsertCpDialog()
{
Type.enableInterface(this, "Tridion.Cme.Views.InsertCpDialog");
this.addInterface("Tridion.Cme.Views.DashboardBase");
};
Tridion.Cme.Views.InsertCpDialog.prototype.initialize = function InsertCpDialog$initialize()
{
}
Edit
hi frank thank you, but already i am using same thing in my code to get the list of componet and template listed on a page under CP tab.
function getbtn() {
//alert('inside getbtn');
var sbtn = document.getElementById ("buttonComponentInsert");
$evt.addEventHandler(sbtn , "click", getListofCPBtnClick);
}
function getListofCPBtnClick(e) {
//code will go here
}
My question is :
I need to get selected Component and template Id from Insert CP window.Earlier i was able to get that by changing the CME extension standard code, but i am not suppose to do that, So first i am trying to initialize the "insert CP window" from my javascript code. I can create event handler for that window, but my question is how to initialize that so that i can call any function once it get initialize. Kindly let me know if i anot clear.
Is your script getting loaded into the dialog?
If not, Albert shows how to do that here: http://albertromkes.com/2012/01/30/tridion-gui-extensions-how-to-load-a-javascript-without-showing-a-gui-element/
He then also shows how to listen to events to accomplish something similar to what you are trying to do.
Ok so I've got my template in its own file named myApp.html. My template code is as follows
<template name="initialInsertion">
<div class="greeting">Hello there, {{first}} {{last}}!</div>
</template>
Now I want to insert this template into the DOM upon clicking a button. I've got my button rendered in the DOM and I have a click event tied to it as follows
Template.chooseWhatToDo.events = {
'click .zaButton':function(){
Meteor.ui.render(function () {
$("body").append(Template.initialInsertion({first: "Alyssa", last: "Hacker"}));
})
}
}
Now obviously the $("body").append part is wrong but returning Template.initialInsertion... doesn't insert that template into the DOM. I've tried putting a partia {{> initialInsertion}}but that just errors out because I dont have first and last set yet... any clues?
Thanks guys
In meteor 1.x
'click .zaButton':function(){
Blaze.renderWithData(Template.someTemplate, {my: "data"}, $("#parrent-node")[0])
}
In meteor 0.8.3
'click .zaButton':function(){
var t = UI.renderWithData(Template.someTemplate, {my: "data"})
UI.insert(t, $(".some-parrent-to-append"))
}
Is first and last going into a Meteor.Collection eventually?
If not, the simplest way I know is to put the data into the session:
Template.chooseWhatToDo.events = {
'click .zaButton' : function () {
Session.set('first', 'Alyssa');
Session.set('last', 'Hacker');
}
}
Then you would define:
Template.initialInsertion.first = function () {
return Session.get('first');
}
Template.initialInsertion.last = function () {
return Session.get('last');
}
Template.initialInsertion.has_name = function () {
return Template.initialInsertion.first() && Template.initialInsertion.last();
}
Finally, adjust your .html template like this:
<template name="initialInsertion">
{{#if has_name}}
<div class="greeting">Hello there, {{first}} {{last}}!</div>
{{/if}}
</template>
This is the exact opposite solution to your question, but it seems like the "Meteor way". (Basically, don't worry about manipulating the DOM yourself, just embrace the sessions, collections and template system.) BTW, I'm still new with Meteor, so if this is not the "Meteor way", someone please let me know :-)
I think you may want to use Meteor.render within your append statement. Also, note that if you are passing data into your Template, then you must wrap Template.initialInsertion in an anonymous function, since that's what Meteor.render expects. I'm doing something similar that seems to be working:
Template.chooseWhatToDo.events = {
'click .zaButton':function(){
$("body").append(Meteor.render(function() {
return Template.initialInsertion({first: "Alyssa", last: "Hacker"})
}));
}
}
Hope this helps!
Many answer here are going to have problems with the new Blaze engine. Here is a pattern that works in Meteor 0.8.0 with Blaze.
//HTML
<body>
{{>mainTemplate}}
</body>
//JS Client Initially
var current = Template.initialTemplate;
var currentDep = new Deps.Dependency;
Template.mainTemplate = function()
{
currentDep.depend();
return current;
};
function setTemplate( newTemplate )
{
current = newTemplate;
currentDep.changed();
};
//Later
setTemplate( Template.someOtherTemplate );
More info in this seccion of Meteor docs