Backbone collection.create not triggering 'add' event - events

I'm struggling with a simple backbone/coffeescript example. I'm trying to add a Question to the QuestionList and have it fire an 'add' event on the collection so I can render it. I'm using the create method since I am trying to have it connect to my server, here modelled by the console.
In this example the console prints "create: {"question":"Question","answer":"Answer"}" but not "Event occurred" as expected. What am I doing wrong here?
jQuery ->
class Question extends Backbone.Model
defaults:
question: 'Question'
answer: 'Answer'
class QuestionList extends Backbone.Collection
model: Question
initialize: ->
#bind 'all', -> console.log "Event occurred"
Backbone.sync = (method, model) ->
console.log method + ": " + JSON.stringify(model)
question_list = new QuestionList
question_list.fetch()
question_list.create
question: $('#question').val()
answer: $('#answer').val()

The version of Backbone being used was old since I'd downloaded a tutorial and continued working from there. Upgrading to Backbone 0.9.9 solved the issue. (thanks Fencliff!)

Related

Followed different suggestions but still get "Invalid hook call. Hooks can only be called inside of the body of a function component"

I'm getting
Error: Invalid hook call. Hooks can only be called inside of the body
of a function component.
I followed different suggestions here on other questions and also on other forums. The last I tried is this one, but still I can't figure out what's the problem with my code:
function myFunc() {
const locale = useSelector((state) => state.locale);
return <></>;
}
I am really new to react so don't hesistate to tell me if you need me to post further details

presentViewController does not exist

I am building a plugin in NativeScript.
When I try to access "presentViewController" method on rootViewController, I get the error "property presentViewContrller does not exist".
const rvc = UIApplication.sharedApplication.keyWindow.rootViewContrller;
rvc.presentViewContrller(myViewController, true, completion() {});
It suggests to use presentViewContrllerAnimatedCompletion which does not accept my view controller.
Could you please assist what part of my code (or maybe setup!) is wrong?
The right method name is presentViewControllerAnimatedCompletion only.
You should combine the parameter names with method name while marshalling Objective C to JS/TS.
presentViewController:animated:completion

Symfony and Swift Mailer

https://swiftmailer.symfony.com/docs/introduction.html
Documents are lacking some information, like all symfony documents are but my problem at moment is that how to instantiate the new Swift_SmtpTransport()?
I cannot find namespace for it. If i do like the document says, i get this error -> Attempted to load class "Swift_SmtpTransport" from namespace App\Controller.
plus, documents don't say how to instantiate Swift_Message class either.
Trying container hasn't worked for me, like so -> $this->container->get
I think the answer is here if you want to send a message -> Attempted to call an undefined method named "newInstance" of class "Swift_Message"
I quote :
Now it is:
$message = \Swift_Message::newInstance()->setSubject('Hello Email')
instead of
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')

Method SessionHelper::write does not exist Error

I am trying to use sessions in my CakePHP 2.3 application. When I add this to my View:
$this->Session->write('key','value');
I get the following error:
Warning (512): Method SessionHelper::write does not exist [CORE\Cake\View\Helper.php, line 179]
I have tried adding this to my controller:
var $helpers = array('Html', 'Form', 'Js'=>array("Jquery"),"Session");
public $components = array('RequestHandler','Session');
But the error still occurs. Anyone know what is going on?
thanks
From the documentation:
The major difference between the Session Helper and the Session Component is that the helper does not have the ability to write to the session.
and so there is no write() method available in the SessionHelper (which you can also see in the API).
Try changing
var $helpers = array('Html', 'Form', 'Js'=>array("Jquery"),"Session");
to
public $helpers = array('Html', 'Form', 'Js'=>array("Jquery"),"Session");
Also, I would recommend doing all session writing login in the controller, not in the view. The view is intended just to display things. So avoid it if you can.

Symfony2: Change rendered view with a listener

I would like to render different views in different context in my Symfony2 project.
I'm using multiple routes for the same actions and I would like to render a different page (view) but with the same controller.
For example I have:
#Route("/articles/show", name="articles_show")
#Route("/mobile/articles/show", name="mobile_articles_show")
Both routes are using the same action : ArticlesController:showAction(), but should render 2 differents templates (for mobile users and regulars ones).
show.html.twig
mobile.show.html.twig
I do not want to use a if statement or whatever in my controller, so I created a listener (similar to a preExecute function)
Here is a part or my config.yml that defines my listener
services:
controller.pre_execute_listener:
class: MyProject\MyBundle\Listener\ControllerListener
arguments: ["#security.context", "#doctrine", "#router", "#session"]
tags:- { name: kernel.event_listener, event: kernel.controller, method: preExecute }
I was thinking about doing something like that in the listener preExecute function:
if(substr($route,0,7) == 'mobile_'){
$view = 'mobile.'.$view;
}
Unfortunately I cannot find a way to get $view or update the view "on the fly", just before it's rendered.
I hope my question is clear enough, thanks in advance, any idea is welcome :)
J.
Here is the solution:
First I have to listen to kernel.view, not kernel.controller.
Then I use the "#templating" service (Thanks Marko Jovanovic for the hint)
So here is my new config.yml:
services:
controller.pre_execute_listener:
class: MyProject\MyBundle\Listener\ControllerListener
arguments: ["#templating"]
tags:
- { name: kernel.event_listener, event: kernel.view, method: preExecute }
Finally here is my listener preExecute function
public function preExecute(\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event){
//result returned by the controller
$data = $event->getControllerResult();
/* #var $request \Symfony\Component\HttpFoundation\Request */
$request = $event->getRequest();
$template = $request->get('_template');
$route = $request->get('_route');
if(substr($route,0,7) == 'mobile_'){
$newTemplate = str_replace('html.twig','mobile.html.twig',$template);
//Overwrite original template with the mobile one
$response = $this->templating->renderResponse($newTemplate, $data);
$event->setResponse($response);
}
}
Hope this helps!
J.
Worth noting: The accepted solution doesn't actually work if you directly return a Response-object (e.g. when you call $this->render()), because the kernel.view event is not fired in that case:
If the controller doesn't return a Response object, then the kernel dispatches another event - kernel.view.
— see Symfony's HTTP Kernel Docs
I couldn't work out a way around this, but found another interesting solution for the same problem:
You could simply extend twig's render engine like the ZenstruckMobileBundle does or write your own file locator like the LiipThemeBundle.
[edit:] Alternatively you could also override the TemplateNameParser.
You can add "#templating" service as argument for the controller.pre_execute_listener.
It seems your device detection is done before you come to your route, so I bet you expect that mobile user will use the mobile routes thanks to some detection before the request, this seems to be painful to deal with in every templates and url generation.
May be better to detect device either before or later (thanks to Categorizr or some nice apache configuration) but not relying on used route for the mobile detection.
An integration of Categorizr with that way of calling templates rendering might be nice.
Then using a nice bundle for using the right templates/themes or using one which provides some more generic functions

Resources