SAILS.JS
I have two forder in controller: admin and public.
I want to edit view.js file in config forder.
if controllers file in admin forder, it call a layout: layout-admin
if controllers file in public forder, it call a layout: layout-public
but i don't know do it.
please support for me with this. thank a lot!
You can do what you want, look the doc here : http://sailsjs.org/documentation/reference/configuration/sails-config-views
The layout attributs can only be a string or a boolean, there no way actually to define a layout with a function or for an entire controller.
You can make a feature request to sails to see this feature in a next version.
You can specify layout file in your controller like this :
myAction : function (req, res)
{
var layout = "layout-public";
if(req.session.authenticated)
{
layout = "layout-admin";
}
res.view("myview", {
layout : layout
});
}
Related
I am working on a personal project for a photography web application.
Still very new to node.js and express.
The previous ASP.Net MVC app I built for this application used a SQL database to index photos and auto-generate the neccesary photo gallery markup.
node-gallery
seemed like a good candidate to remove the db dependency.
However I seem to be confused as to how Express middleware works.
var nodeGallery = require('node-gallery');
//
//
app.use('/', new nodeGallery({
staticFiles : 'public/photos/slideshow',
urlRoot : 'photos/slideshow',
render : false
}), function(req, res, next){
rerurn res.render('index',{slideshow:req.data});
});
app.use('/portfolio', new nodeGallery({
staticFiles : 'public/photos/portfolio',
urlRoot : 'portfolio',
render : false
}), function(req, res, next){
console.log(req.data);
return res.render('portfolio',{portfolio:req.data});
});
I am wanting to use the node-gallery middleware with different properties for two pages (the front with a slideshow and the main gallery). However the last properties set are always used regardless of route.
Using express.Router and specifying a route which uses the middleware also appears to not work.
It seems like I am missing something basic here. Any suggestions would be appreciated.
var nodeGallery = require('node-gallery');
Think of this as creating a class/obj of node gallery.
Then in your first route you set its properties as:
new nodeGallery({
staticFiles : 'public/photos/slideshow',
urlRoot : 'photos/slideshow',
render : false
})
but in the second route you overwrite nodeGallery with a second set of properties:
new nodeGallery({
staticFiles : 'public/photos/portfolio',
urlRoot : 'portfolio',
render : false
})
The problem is you are using the same instance of nodeGallery, to define to sets of different properties.
Solution:
1. Create an object of multiple node-galleries i.e:
var nodeGallery = {
slideShow: require('node-gallery'),
mainGallery: require('node-gallery'),
}
and then access them as nodeGaller.slideShow({staticFiles....})
2. the better solution in my opinion would be to use the recommended way on the github page:
app.use('/gallery', require('node-gallery')({
staticFiles : 'resources/photos',
urlRoot : 'gallery',
title : 'Example Gallery'
}));
you're not setting node-gallery to any one variable, just initializing for that route.
I have the following url structure:
www.mysite.com/temporary/articles.php/artid=1
I would like to change it with:
www.mysite.com/temporary/articles/article-title-here.
where article-title should be based on artid .
Anyone can tell me how can I do that?
No .htaccess needed. I would create another controller method and add some routes. I would use the following approach:
You submit the following URL: www.mysite.com/temporary/articles.php/artid=1
CI catches it and reroutes it as such:
In your routes.php:
$route['temporary/articles.php/artid=(:any)'] = "temporary/articles/search_by_id/$1";
In your Controller:
class Articles extends CI_Controller {
public function index($title){
//load your model
//from your model, query your database to get your article info by title
//send results to your view.
}
public function search_by_id($id){
//load your model
//from your model, query your database to get your article title by id. Set it = $title
redirect("temporary/articles/$title")
}
}
I am creating an NodeJs + AngularJS Application. I have list of Hotels stored into database. I want to create dynamic route based on the hotel name and the load partial view based on property ID.
E.g In my database I have:
HotelID HotelName
1 example hotel
2 second example hotel
3 third example hotel
In app.js I want something like this
var hotelierApp = angular.module('hotelierApp', ['ngRoute', 'ngCookies', 'pascalprecht.translate', 'hotelierApp.services', 'hotelierApp.directives', 'hotelierApp.filters', 'hotelierApp.controller']);
hotelierApp.run(function ($rootScope) {
$rootScope.langId = 1;
})
hotelierApp.config(['$routeProvider', '$locationProvider', '$translateProvider',
function ($routeProvider, $locationProvider, $translateProvider) {
angular.forEach(hotels, function (hotel) {
$routeProvider.when(hotel.name.replace(" ","-"), { templateUrl: 'partials/property', controller: propertyCtrl });
});
angular.forEach(reviews, function (review) {
$routeProvider.when(review.title.replace(" ","-"), { templateUrl: 'partials/review', controller: reviewCtrl });
});
$locationProvider.html5Mode(true);
$translateProvider.useStaticFilesLoader({
prefix: 'data/locale-',
suffix: '.json'
});
$translateProvider.preferredLanguage('en');
$translateProvider.useLocalStorage();
}
]);
Here Hotels/reviews will be the list coming from database by making api calls and also I want to pass their corresponding Ids as route params to the controller.
I have many other section in my application for which i have to create routes from database.
Please help.
Regards,
- Manoj
There is no reason you need to do that.
i'm pretty sure your routes can be factored into something like
$routeProvider.when("/:id/:name", {
templateUrl: 'partials/property', controller: propertyCtrl })
then use $routeParams to get name of the hotel in your controller.As for template urls,your can pass a function instead of a string that will resolve the name of the template you need to use.
templateUrl:function(pathParms){...}
So no need to use angular.forEach.
While this is using another router: http://dotjem.github.io/angular-routing/ the same stuff is possible in tne core router.
Here is illustrated what #mpm is saying you can do: http://plnkr.co/edit/8XuJswpx0FucwMczWTHF?p=preview
In your case I think Option1 would be most appropriate, this is because you talk about a generated url pr. hotel, which makes me assume that there is a large similarity in layout for all hotels.
When that is the case, they may as well share template, where you can just populate the template with the hotel specific data.
The current project I'm on is utilizing tenant sites. With each site, we want the ability to change the logo through out the tenant site by modifying the its settings (on the admin page, settings > general).
I've added two text fields to the site settings by following this well documented tutorial. However, I'd like the user to be able to pick the logos using the media picker instead of typing in the path.
Currently I have a LogoBarSettings part with its record, driver and handler. I'm not sure how to add the media picker to the my LogoBarSettings and even if I did, must I also create another handler, driver, and record for it? I can't imagine I would but I'm pretty stuck at this point.
Can someone provide some direction on this?
Here is my LogoBarSettings
public class LogoBarSettings : ContentPart<LogoBarSettingsPartRecord>
{
public string ImageUrl
{
get { return Record.ImageUrl; }
set { Record.ImageUrl = value; }
}
public string ImageAltText
{
get { return Record.ImageAltText; }
set { Record.ImageAltText = value; }
}
}
The MediaPicker is invoked through Javascript, so you shouldn't need to change any of your model classes. When the MediaPicker is loaded for a page, it sets up a jQuery event handler for all form elements on the page. Triggering the event orchard-admin-pickimage-open will open the MediaPicker. Supply a callback function to capture the picked media.
Here is a quick example that you can run in Firebug or Chrome Developer Tools from a page which has the MediaPicker loaded, such as a Page editor:
$('form').trigger("orchard-admin-pickimage-open", {
callback: function(data) {
console.log(data);
}})
This should print something similar to this:
Object {img: Object}
img: Object
align: ""
alt: ""
class: ""
height: "64"
html: "<img src="/Media/Default/images/test.jpg" alt="" width="64" height="64"/>"
src: "/Media/Default/images/test.jpg"
style: ""
width: "64"
__proto__: Object
__proto__: Object
The BodyPart editor integrates Orchard's MediaPicker with TinyMce, so you can start looking at that module for a more complete example, specifically Modules\TinyMce\Scripts\plugins\mediapicker\editor_plugin_src.js.
I have been looking at the Todo list example (source) for Backbone.js. The code uses local storage, and I wanted to try and convert it so that it operated via a RESTful webservice.
Suppose the webservice already exists at the route todos/. I figured I need to add in a url piece into Backbone.Model.extend and remove the localStorage: new Store("todos") line when we perform Backbone.collection.extend.
window.Todo = Backbone.Model.extend({
url : function() {
return 'todos/'+this.id;
}
// Default attributes for a todo item.
defaults: function() {
return {
done: false,
order: Todos.nextOrder()
};
},
// Toggle the `done` state of this todo item.
toggle: function() {
this.save({done: !this.get("done")});
}
});
What is the proper way to do this?
Url should be set in Collection, if you have need for diferent urls than those created by collection than declare url in model.
You need to remove
<script src="../backbone-localstorage.js"></script>
from index.html since it is linked after backbone.js and effectively overrides Backbone's sync method to store in localStorage.
I would leave the model as it is in the Todos example. In the collection class add this property:
window.TodoList = Backbone.Collection.extend({
...
url: '/todos',
...
}
Calling fetch() on the collection should retrieve a list of Todo objects.
If you are using Rails you need to set ActiveRecord::Base.include_root_in_json = false otherwise Backbone.js will not be able to pull out the Todo objects from the returned json.